Thread: correct this code please

  1. #1
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159

    Question correct this code please

    this code is correct else correct me

    Code:
    char p[1];
    
    scanf("%s",&p[1]);
    
    prtintf("%s",&p[]);

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Very much NOT correct.
    Code:
    char p[1];
    Fine but pointless
    Code:
    scanf("%s",&p[1]);
    You are trying to read in a null terminated string and put it into a ONE character. Even worse your index on the array is wrong. it should be [0].
    Code:
    prtintf("%s",&p[]);
    Should be printf() and you are trying to print a null terminated string giving it one character. However even that is incorrect since you didn't give an index to use.

  3. #3
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    you need room in your arrays because all they are are strings characters pointing to the next one then a '\0' at the end
    so say you do
    Code:
    char array[]="Strings";
    Code:
    array[0]='S';
    array[1]='t';
    array[2]='r';
    array[3]='i';
    array[4]='n';
    array[5]='g';
    array[6]='s';
    array[7]='\0';
    so your code creates room for one character a '\0' character and that is it. Do you understand

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    1
    hehe
    p[0]=your first num
    p[1]=the second ......I only can answer u so much!(我仅能回答你这些)!

  5. #5
    /*enjoy*/
    Join Date
    Apr 2004
    Posts
    159
    thank you all my code it's o.k now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my Dictionary removal code correct?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-17-2008, 08:45 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM