Thread: how does this work?

  1. #1
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534

    how does this work?

    I copied this out of the FAQ - not to criticise, but to ask a question, and since it is easier to cut and paste...

    Code:
    int main()
    {
      char buf[BUFSIZ];
      char *p;
      
      printf ("Please enter a line of text, max %d characters\n", sizeof(buf));
      
      if (fgets(buf, sizeof(buf), stdin) != NULL)
      {
        printf ("Thank you, you entered >%s<\n", buf);
        
        /*
         *  Now test for, and remove that newline character
         */
        if ((p = strchr(buf, '\n')) != NULL)
          *p = '\0';
          
        printf ("And now it's >%s<\n", buf);
      }
      
      return 0;
    }
    The part I am wondering about is this:

    Code:
    char *p;
    looks like a declaration of a char pointer 'p'. Its not pointing to anything, is it? Then p is used later without ever making it point to anything? If someone would explain how this works, I would appreciate it. Its the last part of this code that I don't quite get.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >looks like a declaration of a char pointer 'p'. Its not pointing to anything, is it? Then p is used later without ever making it point to anything?
    Code:
    if ((p = strchr(buf, '\n')) != NULL)
    I think you just made an argument against the use of embedded assignments.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    looks like a declaration of a char pointer 'p'. Its not pointing to anything, is it?
    It is pointing to something
    Code:
    if ((p = strchr(buf, '\n')) != NULL)
    The return value of strchr
    return a pointer to the matched character or NULL if the character is not found.
    At the same time the result is tested to see if it is NULL.

    So say buf is "ABCDEFGH\nabcdefgh" after the statement p will be pointed to to the address of buf[8]. If you were looking for I p would be pointed to NULL.

    I think you just made an argument against the use of embedded assignments
    Often checking the result in the same line as the assignment actually makes a lot of since. That way you can link the operation with the result and you get to store it.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Thantos
    I think you just made an argument against the use of embedded assignments
    Often checking the result in the same line as the assignment actually makes a lot of since. That way you can link the operation with the result and you get to store it.
    Yes, it does make a lot of sense, from an operation standpoint, but I think what Dave was referring to is from the readability standpoint it's easy to miss or misinterpret the statement. So it may be better to change
    Code:
    if ((p = strchr(buf, '\n')) != NULL)
    to
    Code:
    p = strchr(buf, '\n');
    if (p != NULL)
    for readability.

    This is harder to do though when the statement is a while instead of if
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Reminds me of a segment of code in Bjarne Stroustrup's C++ book
    Code:
    void copy (char* p, const char* q)
    {
      while(*p++=*q++);
    }
    Once you know the individual parts it actually makes a lot of sense and is just as easy to read.

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Ok great, I understand it now. Just after I asked, I was thinking about the man page for strchr and something about it returning a pointer, but I still was not clear on it. Thanks.

    ~/

  7. #7
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Originally posted by Thantos
    It is pointing to something
    Code:
    if ((p = strchr(buf, '\n')) != NULL)
    The return value of strchr
    At the same time the result is tested to see if it is NULL.

    So say buf is "ABCDEFGH\nabcdefgh" after the statement p will be pointed to to the address of buf[8]. If you were looking for I p would be pointed to NULL.
    So another way of saying strchr returns a pointer would be to say that it returns the address of the array element, which in the case of your example is buf[8]?

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by kermit
    So another way of saying strchr returns a pointer would be to say that it returns the address of the array element, which in the case of your example is buf[8]?
    Correct.

    >>from the readability standpoint it's easy to miss or misinterpret the statement<<
    ... it's also a very common convention, people need to be used to using it. Even if you don't use it in your own code, most other programmers will.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM