#1
  1. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13

    basic question about pointers in C


    (I want to know some extremely basic facts about pointers in C, but first of all, I want to let you know that I understand what pointers are. Their value, or what they hold, is an address to a piece of data or a variable.)

    What should a pointer's type be defined as? Is an address in C correlated to the type of data stored at that address? In other words, does a pointer's type have to equal the type stored at the address it is pointing to upon declaration?

    For example, if I want a pointer to a char and a long int, is this how I should do it?

    // regular variables
    char c;
    long int i;

    // pointers
    char *cp
    long int *ip;

    What confuses me is that if this is true, I just created two pointers with the first being a byte in size, and the second being four bytes in size. What am I misunderstanding? Does declaring a pointer using "char" or "long int" not effect the actual size of the pointer itself, but instead 'tells' the program that those pointers are pointing to variables of that size instead? If so, what confuses me with that is how can I have a pointer to char point to a full null-terminated string that is bigger than a char?

    Sorry, I am just a bit rusty with programming C after progrmming all this perl code...
    Last edited by Doucette; February 4th, 2003 at 08:14 PM.
    Matthew Doucette / Xona.com
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Location
    Blacksburg VA/Philly PA
    Posts
    38
    Rep Power
    12
    pointers are trickier than they should be. when you declare something such as:

    double Target = 10;
    double* tPtr = Target;

    you first declare a double Target which takes up 8 bits of memory (i believe don't kill me if i am wrong), and then next you declare a pointer; tPtr which points to Target. the act of pointing stores the memory address of Target into tPtr. the memory allocated for the storage of memory addresses is 4 bits (i believe as well).

    no matter what target you are pointing to, a memory address will be stored in the pointer.
  4. #3
  5. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    Thanks pschmerg, I am fairly certian that the actual size of the pointer itself stays the same size too. Let me ask directly where I am stuck with pointers:

    I have this declared...

    char *data;

    ...and it points to a string. The pointer is derived from getenv(), incidentally. Anyway, this prints the string...

    printf("%s",data);

    ...but how do I extract each individual character out of the string? This is where I am lost.
    Matthew Doucette / Xona.com
  6. #4
  7. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    char *pString=NULL;

    //fill pString with data

    while( *pString!='\0' && *pString!=',' )
    {
    *pString++;
    }
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247

    Re: basic question about pointers in C


    Originally posted by Doucette
    Does declaring a pointer using "char" or "long int" not effect the actual size of the pointer itself, but instead 'tells' the program that those pointers are pointing to variables of that size instead? If so, what confuses me with that is how can I have a pointer to char point to a full null-terminated string that is bigger than a char?
    Yep, right on the mark there. Not only that, it also tells the compiler by how many bytes to increment the pointer, when you increase the pointer by 1.

    For example:
    Code:
    char ch[5] = { 'a', 'b', 'c', 'd', 'e'};
    double dbl[5] = { 1, 2, 3, 4, 5};
    
    char *pChar;
    double *pDouble;
    
    /* Assign the pointers to the array */
    pChar = ch; /* or &ch[0], if you like */
    pDouble = dbl; /* or &dbl[0] if you like */
    
    /* Print the pointers and the values they point to */
    printf("Addresses: pChar = %d, pDouble = %d\n", pChar, pDouble);
    printf("Value of *pChar = %c, *pDouble = %lf\n", *pChar, *pDouble);
    
    pChar++; /* Point to the next element in the array */
    pDouble++; /* Point to the next element in the array */
    /* You can also do pDouble = pDouble + 1 or whatever */
    
    /* Print the pointers and the values they point to */
    printf("Addresses: pChar = %d, pDouble = %d\n", pChar, pDouble);
    printf("Value of *pChar = %c, *pDouble = %lf\n", *pChar, *pDouble);
    In the above code, we assign two 5 element arrays and assign two pointers to point to the first element of each array. Then we print out the addresses that the pointers point to, and the values in the addresses.

    Now comes the big kicker --- Note that we increment both pointers by 1. Now we print the new addresses that the pointers point to, as well as the values at the addresses. If you're compiling this on a 32 bit compiler, you might notice that pChar goes up by 1 byte, but pDouble goes up by 8 bytes! The reason for this is that the compiler knows that pDouble is a pointer to a double and hence it increases the value of the address by the size of a double (i.e.) 8 bytes.

    Hope this helps! :)
  10. #6
  11. No Profile Picture
    .
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2002
    Posts
    296
    Rep Power
    12
    declaring a pointer using "char" or "long int" not effect the actual size of the pointer itself, but instead 'tells' the program that those pointers are pointing to variables of that size instead
    so you could look at pointers as another variable type (like char or int etc) i suppose. they've got a particular storage capacity associated with them. but also have a particular use.


    i've read a few times that in general using pointers is much more efficient than not. this efficiency must be referring to speed right? cause they're certainly not smaller. easily twice the size of what it's pointing to, and easily more than twice. at first glance efficiency isn't the first thing that springs to mind.

    i guess speed is more important than size normally, so if that's the case, it's a good idea to use pointers and that's where the efficiency description comes from? if size is your main concern then not using pointers would be a good idea?
  12. #7
  13. No Profile Picture
    matthewdoucette.com
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2002
    Posts
    635
    Rep Power
    13
    I am concerned only with optimizations that increase speed, not shrink size. I think size is normally not an issue.
    Matthew Doucette / Xona.com

IMN logo majestic logo threadwatch logo seochat tools logo