Pointer confusion...

This is a discussion on Pointer confusion... within the C Programming forums, part of the General Programming Boards category; char *ptr; char temp[5]; temp="HELLO"; ptr=temp; and here i have a function which sends in this pointer... functionPtr(*ptr); void functionPtr(char ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    Pointer confusion...

    char *ptr;
    char temp[5];
    temp="HELLO";
    ptr=temp;

    and here i have a function which sends in this pointer...

    functionPtr(*ptr);

    void functionPtr(char **ptr){

    *ptr = '\0';

    }

    i'm quite confused now, is the line of code *ptr = '\0' pointing to the pointer in the main program or is it pointing directly to temp?
    Only by the cross are you saved...

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    22
    I think you mean..

    char *ptr;
    char temp[5];
    temp="HELLO";
    ptr=&temp;

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    oop, yeah ok, but can anybody answer my question?
    Only by the cross are you saved...

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    22
    I'll give it a try, you have two levels of indirection. You are sending a pointer to a pointer. You can also go as far as saying ***var and on until you get a headache. For example:

    int nX=10;
    int *p1=&nX;
    int **p2=&p1;
    **p2=9; //this changes the value of nX to 9

    p2 is a pointer that points to the address of another pointer that points to variable nX. This kind of indirection can go on and on.

  5. #5
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    char *ptr;
    char temp[5];
    temp="HELLO";
    ptr=&temp;
    No, this is wrong. Look carefully at the assignment of temp to ptr. There is no need to use the & operator in this assignment as temp is an array, "temp" contains the address to the first element of the array.
    temp="HELLO";
    This is a problem, you need to use strcpy() to make this kind of assignment. You cannot use the = operator to assign a string to an array, unless you do so at the time of declaration.
    functionPtr(*ptr);
    You shouldn't dereference the ptr at this point. You want to send the address of the pointer variable like so.
    functionPtr(&ptr);
    Code:
    void functionPtr(char **ptr)
    {
        *ptr = '\0';
    }
    Again, you can't assign a value to a string as you are doing here. Correctly assigning a value to *ptr (using strcpy in this instance), within this function will change the value that ptr in main points to.
    Last edited by foniks munkee; 06-23-2003 at 04:31 AM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    16

    >Even if you use strcpy(), you still have a problem - temp is only 5 >chars, and "HELLO" needs at least 6 chars (remember the \0)



    In C , We need not define the sizeof("Hello")+1 array size.
    char [5]="hello";/* ok in c */

    In c++ , only we have to define one extra char to hold '\0' at end of the char array;
    char [6] ="hello";//ok
    char [5] ="hello" //error in c++

  7. #7
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    n C , We need not define the sizeof("Hello")+1 array size.
    char [5]="hello";/* ok in c */

    In c++ , only we have to define one extra char to hold '\0' at end of the char array;
    char [6] ="hello";//ok
    char [5] ="hello" //error in c++
    Rubbish.

    Salem is quite right. My problem is that I counted "Hello" to have four characters, I did this twice...

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by lsme

    >Even if you use strcpy(), you still have a problem - temp is only 5 >chars, and "HELLO" needs at least 6 chars (remember the \0)

    In C , We need not define the sizeof("Hello")+1 array size.
    char [5]="hello";/* ok in c */
    Entirely incorrect.

    Salem, as pointed out in the post above me, is correct. There is only one legal way to initialize the array at creation time with the word 'hello', using only five bites.

    char foo[5] = { 'h', 'e', 'l', 'l', 'o' };

    Doing so however, does not make 'foo' a string. It is simply an array of characters that are not null terminated. As such, it is incorrect to use it as a string.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Oops - don't know what is going on here.. Can't delete post.. brain hurting..
    Last edited by foniks munkee; 06-23-2003 at 04:30 AM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

  10. #10
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Thats true, but the context of the thread kinda confused the issue.

    Interesting article

    [edit]: Appologies to lsme, you were quite right, I was literally looking at the context of the thread and not what you were saying.
    Last edited by foniks munkee; 06-23-2003 at 06:07 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by vVv
    > Rubbish.

    > Entirely incorrect.

    Not really.
    What lsme said is correct (although in context, his response is probably a little offtopic ): In C, you are allowed to strip the nul-character of a string literal initializer by explicitly specifying the number of array elements to be one byte less than the string would take, while you aren't in C++
    *Looks in the standard.* Well I'll be damned. You learn something new every day. I can't picture myself ever wanting to, but I suppose it's useful to know I could. I guess.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jun 2003
    Posts
    147
    wow, some kind of debate, hehe, but wat i was taught before was that if HELLO has 5 characters, so declaring it as

    char temp[5];
    strcpy(temp, "HELLO");

    so temp[5] actually stores 6 items, 5 of them are characters which make up HELLO, and the remaining one stores the '\0' bit, correct me if i am wrong, is this correct?

    functionPtr(*ptr);

    void functionPtr(char **ptr){

    *ptr = '\0';

    }
    so i'm wrong here, in this context i should be passing in the address of ptr into the functionPtr?
    so it should be

    functionPtr(ptr) instead of passing in *ptr?

    ok, so wat char **ptr will get in the function is the address of the ptr value in main?

    so my line of code in the function

    *ptr = '\0', should be strcpy(*ptr, '\0')?

    btw, i'm really confused, am i assigning a value which is supposed to be an address to the ptr in main? or am i directly assigning a value to the variable temp?
    Only by the cross are you saved...

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by fkheng
    wow, some kind of debate, hehe, but wat i was taught before was that if HELLO has 5 characters, so declaring it as

    char temp[5];
    strcpy(temp, "HELLO");

    so temp[5] actually stores 6 items, 5 of them are characters which make up HELLO, and the remaining one stores the '\0' bit, correct me if i am wrong, is this correct?
    In a word: No. Your array will only ever be 5 characters long, because that's the size you declared it as. Sure, it may add a null to whatever is beyond the end of your array, but that is just that: beyond the end of your array.

    Never ever try to put information beyond the end of your array.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    As to pointers to pointers:
    Code:
    function( char ** ptrptr )
    {
       ... do stuff ...
    }
    If that is your function, it means it wants a pointer to a pointer. That is to say:
    Code:
    char *ptr;
    This is a pointer. Your function wants a poitner to that. Do it one of two ways:
    Code:
    char **ptr2ptr;
    
    ptr2ptr = &ptr
    function( ptr2ptr );
    Or, pass it the address of a pointer:
    Code:
    char *ptr;
    function( &ptr );
    Remember, * dereferences a pointer. That is to say, it gives you what it referres to; not the address of it.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer confusion
    By rohit_second in forum C Programming
    Replies: 1
    Last Post: 10-20-2008, 04:25 AM
  2. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  3. (n00b) Pointer Confusion
    By kidburla in forum C Programming
    Replies: 5
    Last Post: 10-17-2005, 09:35 AM
  4. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 01:19 AM

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