![]() |
| | #1 |
| Registered User Join Date: Feb 2010
Posts: 67
| easyquestion 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 | |
| | #2 |
| Registered User Join Date: Dec 2002
Posts: 518
| Code: scanf("%s",name[i]); // why is this syntex invalid
Code: name[a]=getch(); // how do i show the value the i have imput to the user in the screen. Code: printf("%s",name);
|
| kryptkat is offline | |
| | #3 | |
| Mysterious C++ User 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:
| |
| Elysia is offline | |
| | #4 | |
| Registered User Join Date: Feb 2010
Posts: 67
| this sysntax does work Quote:
| |
| mouse666666 is offline | |
| | #5 |
| dat is, vast staat Join Date: Jul 2008 Location: SE Queens
Posts: 6,612
| 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|