ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In taking the address of an array, use either of two notations:
Code:
scanf( "%s", name );
scanf( "%s", &name[0] );
You are telling scanf() where to put the string data it will store in your variable. The shorthand notation that creates a reference to an array simply by giving the name of the array APPLIES ONLY TO ARRAY VARIABLES. For scalars you must explictly use the address-of operator to create a reference to the variable.
#include <stdio.h>
#include <stdlib.h>
int main(void){
char name[5];
int age;
char gender;
puts("What is your name?");
scanf("%s",&name);
puts("What is your age?");
gets(age);
puts("What is your gender");
gender=getchar();
if(gender=='m'){
puts("You are male");
}
else if(gender=='f'){
puts("You are female");
}
else{
puts("Wrong gender\n!!!!!");
}
}
and this happens when I run it
Code:
What is your name?
n
What is your age?
Segmentation fault
I cannot figure out this segmentation fault thing??
To help yourself understand the problem concept, explain in words what the following line of code is intended to do.
Code:
scanf("%s",&name);
Focus on the nature of the function arguments. Be verbose. If you look at it in enough detail to explain it well, you will probably see the problem. If not, we may get enough insight into your mindset to explain the matter on your terms.
I cannot figure out this segmentation fault thing??
Quote:
Originally Posted by Nabeel
Code:
int age;
<snip>
gets(age);
Guess what the "s" in "gets" stands for. It doesn't stand for "int".
You should really read a C tutorial (there are many online), and read the man pages of the standard C functions if you're not sure how they work. For example:
So the problem is using in gets() function for age?? I had started consulting "C for dummies", It introduced me to gets() and puts() and since they were more easy to type, i started using them. But when I started attempting various exercises well above is what happened. Stuck at the first one.
It introduced me to gets() and puts() and since they were more easy to type, i started using them.
That's not a good reason to use a function. You use functions based on what they do, not on how easy their names are to type. The example probably shows using gets() to get a string. It will not magically work for an int, that's a complately different data type that works in a different way.
Also, from the gets() man page:
Code:
BUGS
Never use gets(). Because it is impossible to tell without knowing
the data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer,
it is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.
So the problem is using in gets() function for age?? I had started consulting "C for dummies", It introduced me to gets() and puts() and since they were more easy to type, i started using them. But when I started attempting various exercises well above is what happened. Stuck at the first one.
Yeah I knew I should have consulted the manuals, but better late then never. Also When I replaced the gets() with scanf(), the program showed some thing like this
Code:
What is your name?
n
What is your age?
77
What is your gender
Wrong gender
!!!!!
I tried replacing the getchar() in the source with scanf() but that too gave another segmentation fault, And It kept going that way till I tucked 'em both in like
Code:
puts("What is your gender [m/f]");
scanf("%c",&gender);
gender=getchar();
and by the way where can I get the manuals for these functions, I tried typing in the terminal
Code:
$ man getchar\(\)
but that said there is no entry for it, I tried looking gcc's manual but there isn't any thing on functions, just commands and switches for the gcc compiler.
Well I went to take a course in c language and now since it is over(Thank havens) I am able to make a few small programs. But not as complex as games, So where should I move on next??
That depends. Are you sufficiently accomplished in C that you are thinking about problems in terms of the algorithms, data structures, etc., or do you still have to think primarily about things like C language syntax? If the latter, you need to continue to develop the skill of general programming in C. You should also start developing an understanding of how to use various APIs (generally embodied in C-callable function libraries), and OS interfaces and APIs.
If you are accomplished enough that you can write basic commandline-driven applications using multiple C source files and object modules, then you probably should start developing some basic GUI-driven applications, to develop a feel for the event-driven GUI idiom. For C, GTK-based GUIs are probably among the easiest to pick up on. The concepts you learn there will transfer well across most GUI toolkits, and (I presume), game building.
Well I just brought this up to thank you guys. Its been a year and half since I started this topic. I knew nothing of programing back then. but thanks to you guys today I am able to write programs and create (simple) games. Although not in C but in python. here is a link to a program I've recently created as an assignment. It is basically utilizing a library "simplegui" created by the RICE university professors. But i'm looking towards rewriting this using the "tk" library. I would like you guys to analyze it and throw any tips you guys might think are useful. Also Since I was never able to write any such code in C,(like I have written in the previous memory game) Should I go back and re-explore C or just stick with Python???
I would like you guys to analyze it and throw any tips you guys might think are useful.
Using single letter variable names everywhere makes the code very hard to understand. You're initializing the global variables once at the top level, and again in the init() function; repetition is bad. Speaking of which, the st() and draw() functions look very repetitive, they could surely be written much more concisely.
Quote:
Also Since I was never able to write any such code in C,(like I have written in the previous memory game) Should I go back and re-explore C or just stick with Python???
It's a lot harder to get working GUI programs in C, probably you should stick with Python for now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.