LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-16-2011, 01:44 PM   #76
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908

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.

--- rod.
 
Old 10-04-2011, 12:16 PM   #77
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
Question

Well Here is another program
Code:
#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??
 
Old 10-04-2011, 12:38 PM   #78
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Nabeel View Post
Well Here is another program
...

I cannot figure out this segmentation fault thing??
You seem to be playing volleyball with us - instead of reading any book on "C".

For starters, read 'man gets'.
 
Old 10-04-2011, 01:15 PM   #79
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.
 
Old 10-04-2011, 01:43 PM   #80
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Nabeel View Post
I cannot figure out this segmentation fault thing??
Quote:
Originally Posted by Nabeel View Post
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:

Code:
man 3 gets
 
Old 10-05-2011, 12:08 PM   #81
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
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.
 
Old 10-05-2011, 12:12 PM   #82
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Nabeel View Post
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.

Last edited by MTK358; 10-05-2011 at 12:19 PM.
 
Old 10-05-2011, 12:19 PM   #83
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Nabeel View Post
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.
No, the problem is that you do not read manuals.
 
1 members found this post helpful.
Old 10-05-2011, 12:42 PM   #84
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
Question

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.


And thanks for Responding to my threads
 
Old 10-05-2011, 12:49 PM   #85
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Nabeel View Post
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
Code:
man 3 getchar
 
Old 01-29-2012, 02:08 AM   #86
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
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??
 
Old 01-29-2012, 12:11 PM   #87
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.
 
Old 01-29-2012, 12:20 PM   #88
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
For game, maybe SDL library is worth a look
http://www.libsdl.org/demos.php
 
Old 11-24-2012, 08:23 AM   #89
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 294

Original Poster
Rep: Reputation: 17
Question

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???

Awaiting your advice.

Kind Regards!
 
Old 11-24-2012, 09:40 AM   #90
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,786

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by Nabeel View Post
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.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help learning programming Fred_mike Programming 49 10-19-2010 08:52 AM
programming learning way siaswar Programming 5 09-29-2009 11:58 AM
Learning C Programming Trizon Programming 8 03-30-2007 12:37 PM
learning programming nin881 Programming 13 10-19-2005 12:17 AM
C programming learning introuble Programming 7 01-03-2005 11:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration