C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-07-2010, 01:10 AM   #1
Registered User
 
Join Date: Feb 2010
Posts: 67
easyquestion

what is the difference between thank you for you help
Code:
char name[50],a ;


scanf("%s",name);

scanf("%s",name[i]);  // why is this syntex invalid


gets(name);


name[a]=getch(); //what is this mean iam i getting a string or array?

Last edited by mouse666666; 02-07-2010 at 02:18 AM.
mouse666666 is offline   Reply With Quote
Old 02-07-2010, 01:20 AM   #2
Registered User
 
kryptkat's Avatar
 
Join Date: Dec 2002
Posts: 518
Code:
scanf("%s",name[i]);  // why is this syntex invalid
scanf is looking for a string not an array of char. meow.

Code:
name[a]=getch(); // how do i show the value the i have imput to the user in the screen.
if i understand you correctly
Code:
printf("%s",name);
should work.
kryptkat is offline   Reply With Quote
Old 02-07-2010, 03:17 AM   #3
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 16,078
>>scanf("%s",name);
Reads a string from the user into the array name (correct). Unsafe. Read SourceForge.net: Scanf woes - cpwiki

>>scanf("%s",name[i])
Incorrect. Tries to read a string from the user into a char whose value has been converted into an address. Double wrong. Plus "i" isn't declared anywhere. Scanf needs a pointer to a buffer where it needs to store the data. A char is not a pointer.

>>gets(name);
Worst thing to do. Ever. Read SourceForge.net: Gets - cpwiki

Furthermore, "a" is a char, yet you use it as an index. Is this intentional? Usually, an integer is used for such purposes, since, by definition, an array contains n elements of integer nature.
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2010 Ultimate, C++0x
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
"Thanks for all your help. It's obvious yall really know what you're talking about when it comes to OOP/C++ stuff."
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 02-07-2010, 05:23 PM   #4
Registered User
 
Join Date: Feb 2010
Posts: 67
this sysntax does work
Quote:
scanf("%s",name[i])
mouse666666 is offline   Reply With Quote
Old 02-07-2010, 05:39 PM   #5
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
Quote:
Originally Posted by mouse666666 View Post
this sysntax does work
No, because name is a pointer to a char array, which is where you would put a string (%s). name[i] would be an element of name, which would be where you would place a single letter (%c).

Since i is totally undefined, I am guessing you are cutting and pasting code. This may seem like a clever idea, but it is actually the least effective way to figure something out. If there were absolutely no documentation or way for you to read about and learn C syntax, then you would be left throwing darts blindfolded by cutting and pasting experimentation. But fortunately there is lots and lots of tutorials, etc, etc, around, so you don't have to do that.
__________________
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge
MK27 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump


All times are GMT -6. The time now is 12:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22