if-else statement condition

This is a discussion on if-else statement condition within the C Programming forums, part of the General Programming Boards category; Instead of Code: scanf("%s",&list[i].name); Use Code: scanf("%s",list[i].name); In the future, remember an array name is the address of the array; ...

  1. #16
    Registered User
    Join Date
    May 2009
    Posts
    2,745
    Instead of
    Code:
    scanf("%s",&list[i].name);
    Use
    Code:
    scanf("%s",list[i].name);
    In the future, remember an array name is the address of the array; so no need most of the time to put an address operator (&) in front of it.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  2. #17
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by YouMe View Post
    I'm not understand, can you show where is it to correct it?..i mean an example
    If you declare a variable like
    Code:
    int x;
    the compiler just allocates enough memory to store a value of this type, but it doesn't clear the memory. So whatever value is stored at this memory location is still there (probably just garbage).

    Code:
    int x = 0;
    initializes the variable x to 0 and then you can be sure that this value stays there as long as you don't change it.

    The problem in your code is that
    Code:
    if(list[i].name != NULL)
    compares the pointer "list[i].name" to the pointer "NULL" and the result is always true because all elements of list have a valid memory address and thus they are unequal to NULL. What you want is to compare the value the pointer "list[i].name" points to to a value which denotes an empty string (usually '\0' as the only character in the string). But you have to initialize your list-array to be able to do this.

    I also suggest reading a tutorial about pointers, arrays and strings.

    Bye, Andreas

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to implement this condition..
    By dswartz2 in forum C Programming
    Replies: 11
    Last Post: 05-23-2011, 03:01 PM
  2. if condition
    By jgtech in forum C Programming
    Replies: 1
    Last Post: 04-12-2011, 06:25 AM
  3. If condition
    By rits in forum C Programming
    Replies: 3
    Last Post: 09-02-2009, 06:54 AM
  4. Complicated Condition
    By zmi in forum C++ Programming
    Replies: 4
    Last Post: 05-05-2009, 07:15 PM
  5. c++ condition use? how u use it? help
    By mikeasianlee in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2004, 09:44 PM

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