Pointers confusing me (Newbie questions)

This is a discussion on Pointers confusing me (Newbie questions) within the C Programming forums, part of the General Programming Boards category; My professor is having us define methods (which he has provided the headers for), and they take in: Code: char ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    16

    Pointers confusing me (Newbie questions)

    My professor is having us define methods (which he has provided the headers for), and they take in:

    Code:
    char *indicator
    as a parameter.

    This is a pointer named "indicator" of type char... right? The problem is that in the problem he refers to it as a string. "Take the 4th character in indicator and (do action)" So apparently, somehow in his test program he makes one character in to an array of characters (a string)?

    So I manipulate it as an array of characters.... but I just get confused. My code works, but I want to understand the logic behind it. How does one char suddenly become an array of chars? Furthermore, if I want to get the 4th character in the "string", why would I use

    Code:
    indicator[4]
    instead of

    Code:
    *indicator[4]
    (which gives me an error)

    Doesn't using the variable name without the "*" give me an address? Won't I have to dereference it to get the actual char?

    Another problem asks us how to go about getting the memory address of indicator. Would I then use "*indicator"?

    I'm just confused... someone help?

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by klawson88
    This is a pointer named "indicator" of type char... right?
    No, it is of type pointer to char.

    Quote Originally Posted by klawson88
    The problem is that in the problem he refers to it as a string. "Take the 4th character in indicator and (do action)" So apparently, somehow in his test program he makes one character in to an array of characters (a string)?
    So, the pointer named indicator is supposed to point to the first character of a string that is at least four characters long.

    Quote Originally Posted by klawson88
    Doesn't using the variable name without the "*" give me an address? Won't I have to dereference it to get the actual char?
    Yes, but indicator[4] is equivalent to *(indicator + 4), which is not the same as *indicator[4], which is an attempt to dereference the character at index 4.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by laserlight View Post
    No, it is of type pointer to char.


    So, the pointer named indicator is supposed to point to the first character of a string that is at least four characters long.
    I don't understand. If it is of type "pointer to char" (one character), how exactly are you able to manipulate it like a string (multiple characters)?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,743
    Quote Originally Posted by klawson88
    If it is of type "pointer to char" (one character), how exactly are you able to manipulate it like a string (multiple characters)?
    Because from a pointer to one character in an array you can compute the address of another character in the same array and thus get another (or the same but modified) pointer that you can dereference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Because you can use the "pointer to char" to access the char itself, or even the next char, or the previous char, or the next char. So when you pass a pointer into a function, the function works by accessing the string the pointer is pointing to, not by manipulating the pointer itself.

    It's a whole lot easier to pass around a pointer than to pass the actual string.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by klawson88 View Post
    I don't understand. If it is of type "pointer to char" (one character), how exactly are you able to manipulate it like a string (multiple characters)?
    Maybe because indicator is pointing to a char which is part of an array of char. This way you can use either the array or pointer and an offset notation as laserlight has said in this post.

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    So would this be correct? I'm trying to get the first character in the indicator and hash it:

    Code:
    int hash (int num, char *indicator)
    {
    	int ascii = indicator[0];
    	return ascii%num;
    }
    I just get confused as to what indicator[0] refers to (if it is even correct). If indicator is a pointer to a char (either a single char, or one that is in an array of char), indicator[0] can't possibly be right for what i'm trying to do... because that would be saying that indicator is an array of chars... but its not.

    My professor keeps referring to indicator as a string, so i'm trying to manipulate it as one. Is it, or can it be a string?

    A little less confused, but still confused nonetheless.

    Thanks for all your help so far.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Hmmm that depends on how indicator has been initialized in the calling function. If indicator does point to the first element of an array of char then indicator[0] is correct. This by no means says that indicator is an array of char. Note that a string is the same as an array of char. Ask your professor to explain this topic better before proceeding with the assignments.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by itCbitC View Post
    Hmmm that depends on how indicator has been initialized in the calling function. If indicator does point to the first element of an array of char then indicator[0] is correct. This by no means says that indicator is an array of char. Note that a string is the same as an array of char. Ask your professor to explain this topic better before proceeding with the assignments.
    It doesn't really matter if it's pointing to the first character or not:
    Code:
    #include<stdio.h>
    
    void foo( char *bar )
    {
        if( bar )
        {
            bar[0] = '*';
        }
    }
    
    int main( void )
    {
        char c;
        char baz[ BUFSIZ ] = {0};
    
        foo( &c );
        foo( &baz[ 4 ] );
    
        putchar( c );
        putchar( baz[ 4 ] );
        putchar( '\n' );
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    Quote Originally Posted by itCbitC View Post
    Hmmm that depends on how indicator has been initialized in the calling function. If indicator does point to the first element of an array of char then indicator[0] is correct.
    What? Shouldn't indicator[0] be replaced with "indicator" then (if "indicator" is pointing to the first char in the array, and i'm looking for that)? Why would it be indicator[0]?

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by klawson88 View Post
    What? Shouldn't indicator[0] be replaced with "indicator" then
    Except that indicator[0] is not the same as indicator. The former is a character while the latter is a pointer. Moreover indicator[0] is a relative thing not an absolute one. So if indicator is pointing to some character that is part of an array then you can access that character indirectly as
    Code:
    indicator[0] == *indicator
    indicator[0] by no means is the first item of an array, it's simply whatever item indicator is pointing to.
    Quote Originally Posted by klawson88 View Post
    (if "indicator" is pointing to the first char in the array, and i'm looking for that)? Why would it be indicator[0]?
    It can be either indicator[0] or *indicator.

  12. #12
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    I think it's worth pointing out that the name of an array is treated as a pointer to the first element in the array. So let's say for example you have an array of chars defined like this:

    Code:
    char arr[5] = {'w', 'o', 'r', 'd', '\0'};
    ...the name "arr" by itself, without the brackets, is treated as a pointer to the first element in that array, in this case a 'w'. Dereferencing the name "arr," as in "*arr", gives you the 'w'. So you can say that the name "arr" is a pointer-to-char.

    You can access the other elements in the array by adding to the pointer "arr" like so:

    Code:
    *(arr + 2);
    //will give you the 'r'....
    Of course saying *(arr + 2) is just another way of saying "arr[2]" and so you can see that since the name "arr" refers to a pointer to the first element of the array, the pointer notation and the array notation are equivalent to each other. It's up to you which one you use.

    In your case, you're passing a pointer-to-char, "*indicator," into a function. It points to a char which is the first char in a string. Since you now have access to that pointer in your function, you can use it to access the other characters in the string in either of the two ways mentioned about, i.e. as "*(indicator + n)" or as "indicator[n]". They are both the same.

    Sorry for repeating what's basically already been said but I thought he might find it helpful to hear it from one beginner to another. Plus, I think I'm coming down with swine flu and I've just taken a healthy swig of NyQuil and my head is swimming, so in all likelihood I'll look at this tomorrow morning and think "huh??"

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    AH! It all makes sense now!

    Sharke, the way you explained it is fantastic!

    Thanks to everyone for their help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers newbie... -_+
    By ShadeS_07 in forum C Programming
    Replies: 3
    Last Post: 12-25-2008, 04:13 PM
  2. C++ newbie question about pointers
    By lafayette in forum C++ Programming
    Replies: 15
    Last Post: 09-30-2008, 12:38 PM
  3. newbie question on strings & pointers
    By MK27 in forum C Programming
    Replies: 6
    Last Post: 08-05-2008, 05:50 AM
  4. Some questions about pointers
    By indigo0086 in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2002, 02:25 PM
  5. I have some newbie questions
    By Myra Mains in forum C++ Programming
    Replies: 6
    Last Post: 05-06-2002, 09:30 PM

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