Quick Pointer Question (c++)

Discussion in 'OT Technology' started by D1G1T4L, Feb 28, 2005.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    I have a char *tokenPtr (a pointer that contains a string)

    i want to assign it to an array char a[30];


    how can i do that?




    i am also a little bit confused about pointers as to when to use *variable or just variable name itself

    i understand that you use & to assign address to a pointer but in some examples i see it being used like this

    double *pn;

    pn = &bubble; and use * to get the value of bubble


    but sometimes i see

    *pn = &bubble;


    so i appreciate if someone helps me out with this issue :bigthumb:


    i guess you can say i am confused bout distinguishing between a pointer and the pointed-to value
     
    Last edited: Feb 28, 2005
  2. BoypussY

    BoypussY game over.

    Joined:
    Jun 27, 2000
    Messages:
    51,953
    Likes Received:
    0
    Location:
    IN YOUR HEAD
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Pointers don't contain strings. A pointer contains an address. tokenPtr contains the address of a single char which happens to be the lead char in an array of chars.


    a[0] can only contain a char.
    tokenPtr contains an address of a char, not the char itself.
    Since you're trying to assign an address to a char variable, the compiler isn't happy.

    I'm not sure what you're trying to accomplish. Do you want to copy the entire string that tokenPtr points to to the array a? Or, do you want to copy the single character at the address contained in tokenPtr to a[0]?

    If you want to copy the string into the array a, then you should use string copy.

    strcpy(a, tokenPtr);

    If you just want to copy a single character, then there's a couple ways to do it.

    a[0] = tokenPtr[0];
    or
    a[0] = *tokenPtr;

    Any pointer can be treated as if it points to an array, thus you can use the array index operator [] to access elements that are offset from first address. The compiler doesn't check if there is actually valid data there, so you should be careful.

    The '*' operator or the dereferencing operator is used to retrieve the actual data stored at the address contained by a pointer.

    tokenPtr contains the address of a char.
    *tokenPtr no longer references the char by address, but refers to the char itself.

    a[0] = *tokenPtr; means "assign the char stored at the address contained in tokenPtr and to a[0]"

    *tokenPtr = 'z'; means "assign the letter 'z' to the char stored at the address contained in tokenPtr"

    tokenPtr = a; means "assign the address of the char array a to tokenPtr"

    So, to put it simply, use '*' if you have an address and are interested in the data at the address. Don't use '*' if you're interested in the address itself.
     
  4. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    This would make sense if pn is a pointer to a pointer, otherwise the compiler would complain.

    double bubble;
    double **pn;

    *pn = &bubble; // is valid

    since pn is a "pointer to a pointer," *pn is the pointer pointed to by pn.
     
  5. CalSpeedRacer

    CalSpeedRacer i lurk

    Joined:
    Jun 5, 2001
    Messages:
    3,852
    Likes Received:
    0
    Location:
    sf
    if you get confused, there's nothing wrong with drawing it out on paper using boxes and arrows and walk thru what is going on with each variable. on occasion i still do that if i'm dealing with a really complex data structure.
     
  6. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    thanks a lot guys, you guys rock!
     
  7. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    quick follow up questions

    in my book, they compare 2 arrays like this

    char *s1 = "Happy New Years";
    char *s2 = "Happy New Years";


    strcmp(s1,s2);


    wouldnt that mean they are comparing their "address" and not their actual values?


    i am trying to compare 2 elements of multidimensional array
    by doing this strcmp(array[smallest], array[index])

    however it gives me an error cannot convert paramater 1 from 'char' to 'const char*'

    obviously i cant make the elements const because i'll need to change them around.... any help?
     
  8. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Don't worry, strcmp truly compares the strings. strcmp takes two addresses, then compares the characters stored in the arrays located at the addresses.

    It's not a problem with const. That error message is telling you that your giving strcmp a char when it expects a char*. array[smallest] is a char and not a char* which you assume it is. Make sure you've declared array correctly.
     
  9. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    can u give me an example of comparing 2 multidinesional arrays together?
     

Share This Page