Thread: Program to demonstrate strchr function

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    61

    Program to demonstrate strchr function

    My professor wrote this program below. I was wondering why the find_c variable always changes when I run the program. Also, I'm confused about this line
    Code:
     i = find_c - string2;
    "i" is the difference of the location of the character it's looking for (find_c) and string2, but what what is the value of string2? I was thinking it is the first index of the array because I remember my professor saying that the name of an array points to the first element.

    http://i50.tinypic.com/1r53jo.png

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAXCHAR    256
    
    void main(void)
    {
        int i;
    
        char *find_c;
    
        char string1[] = {"red"};
        char string2[] = {"the color red is good"};
    
    
        find_c = strchr(string2,'r');
    
        i = find_c - string2;
    
        printf("The first %c in \"%s\" occurs at position %d\n", find_c, string2, i);
    
        find_c = strrchr(string2,'r');
    
        i = find_c - string2;
    
        printf("The last %c in \"%s\" occurs at position %d\n", find_c, string2, i);
    
    }        /* end main */

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by november1992
    I was wondering why the find_c variable always changes when I run the program.
    find_c is a pointer. You probably wanted to write *find_c for printing. To be on the safe side, you should check that find_c is not a null pointer.

    Quote Originally Posted by november1992
    "i" is the difference of the location of the character it's looking for (find_c) and string2, but what what is the value of string2? I was thinking it is the first index of the array because I remember my professor saying that the name of an array points to the first element.
    An array is converted to a pointer to its first element.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by laserlight View Post
    An array is converted to a pointer to its first element.
    Um...while I understand you're trying to be brief, this is incorrect and I believe this lack of elaboration is a root of the misconception that arrays are, or the same as, pointers.

    To the OP, an array does is not converted to a pointer. Rather, you get the address of the start of the array when it is unsubscripted. You can assign the address of an array to a pointer, but not vice versa. An array is still a right hand data type.

    Code:
        char *find_c;
     
        char string1[] = {"red"};
        char string2[] = {"the color red is good"};
     
    find_c = string1;  // << is legal.
    string1 = find_c; // << is not legal.
    Last edited by Cynic; 05-03-2012 at 10:30 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Cynic
    Um...while I understand you're trying to be brief, this is incorrect and I believe this lack of elaboration is a root of the misconception that arrays are, or the same as, pointers.
    Incorrect? Uh...
    Quote Originally Posted by C99 Clause 6.3.2.1 Paragraph 3
    Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.
    Quote Originally Posted by Cynic
    an array does is not converted to a pointer. Rather, you get the address of the start of the array when it is unsubscripted.
    An address is (the value of) a pointer.
    Last edited by laserlight; 05-03-2012 at 10:37 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by laserlight View Post
    An address (with an associated type) is a pointer.
    Then by all means, please show an example where you can assign a new address to an array.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Cynic
    Then by all means, please show an example where you can assign a new address to an array.
    You cannot, because the pointer thus obtained is not an lvalue.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by laserlight View Post
    You cannot, because the pointer thus obtained is not an lvalue.
    Because it's not a pointer.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The standard clearly says that you are wrong.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Cynic
    Because it's not a pointer.
    I quoted the standard to you. You're wrong. Period.

    In the given context of november1992's question, the array string2 is converted to a pointer to its first element. This pointer is not an lvalue, but that's fine, because it can still be used in the pointer arithmetic to obtain the index value that is assigned to i.

    EDIT:
    To soften this a bit, I'll clarify by stating that you're wrong to claim that "an array (...) is not converted to a pointer". Otherwise, these statements are correct, or at least can be reasonably interpreted in a way that makes them correct:
    Quote Originally Posted by Cynic
    Rather, you get the address of the start of the array when it is unsubscripted. You can assign the address of an array to a pointer, but not vice versa. An array is still a right hand data type.
    The "start of the array" is its first element. This address comes with a type, i.e., the type of a pointer to an element of the array, hence we get a pointer to the array's first element. "Get" means conversion, since an array is not an address. Denying the conversion from array type to pointer type thus contradicts your own statement.

    It is certainly true that "you can assign the address of an array to a pointer". But here's where type comes into play: a pointer to the array and a pointer to the array's first element have the same value when we are only considering addresses, but they have different types. Since november1992's question is concerned with pointer arithmetic, the type of the pointer comes into play. Hence, just talking about addresses (as numeric values rather than pointers with the notion of type) is insufficient.
    Last edited by laserlight; 05-03-2012 at 11:24 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Mar 2012
    Posts
    61
    You said string2 is converted to a pointer to its first element, so what is that value? And how does it find the position by subtracting from find_c?


    Code:
     i = find_c - string2;
    Code:
      printf("The first %c in \"%s\" occurs at position %d\n", find_c, string2, i);

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You said string2 is converted to a pointer to its first element, so what is that value?
    The address of the first element will be the value of the pointer, of course. When you dereference you get the first element.

    It might be helpful to imagine that memory is a long ruler. This is helpful in understanding how you get completely different addresses with pointer arithmetic, and it should help you visualize the difference between addresses (i.e. the value of any pointer).

    And how does it find the position by subtracting from find_c?
    One way that I think is easy to understand is in the standard.

    Quote Originally Posted by ISO/IEC 9899:TC2 6.5.6.9 footnote 89
    Another way to approach pointer arithmetic is first to convert the pointer(s) to character pointer(s): In
    this scheme the integer expression added to or subtracted from the converted pointer is first multiplied
    by the size of the object originally pointed to, and the resulting pointer is converted back to the
    original type. For pointer subtraction, the result of the difference between the character pointers is
    similarly divided by the size of the object originally pointed to.
    If you need to know the rules about such expressions, that is actually more what 6.5.6.9 is about.
    Last edited by whiteflags; 05-04-2012 at 01:27 PM.

  12. #12
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by laserlight View Post
    An array is converted to a pointer to its first element.
    To be fair, a clearer way of stating that might be "the name of an array decays to a pointer to it's first element" or some such. After all, the "concept" of a pointer is a variable that stores the address of another whereas the "concept" of an array is a contiguous sequence of variables, so saying things like "an array is converted to a pointer to its first element" will invariably lead to confusion, IMO.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with strstr() function
    By zacavitch in forum C Programming
    Replies: 13
    Last Post: 10-12-2009, 02:14 AM
  2. need help with strstr function for microcontroller
    By Lince in forum C Programming
    Replies: 2
    Last Post: 08-07-2008, 08:47 AM
  3. can some please demonstrate fgets ?
    By broli86 in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 02:41 AM
  4. Replies: 14
    Last Post: 03-02-2008, 01:27 PM
  5. strstr help..
    By c2turbo in forum C Programming
    Replies: 7
    Last Post: 09-27-2003, 05:13 PM