easyquestion

This is a discussion on easyquestion within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    96

    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 01:18 AM.

  2. #2
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    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.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,455
    >>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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    this sysntax does work
    scanf("%s",name[i])

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

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