Thread: pointers

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    pointers

    Hey guys if i declare

    Code:
    int *px;
    does that mean px is a pointer to an int

    Code:
    float *pa = &a;
    would that mean the address of &a is of pa which is a pointer to the float

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by bazzano View Post
    Hey guys if i declare

    Code:
    int *px;
    does that mean px is a pointer to an int
    Yes.

    Quote Originally Posted by bazzano View Post
    Code:
    float *pa = &a;
    would that mean the address of &a is of pa which is a pointer to the float
    If a is a float, then &a is the address of the float a, and in the initialization the float pointer pa would have a value of the address of a.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    how about if its this

    double *a[12] would that be a pointer to a pointer of type double because its an array

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by bazzano View Post
    how about if its this

    double *a[12] would that be a pointer to a pointer of type double because its an array
    a is an array of 12 pointers to double. a[0], a[1], etc. is each a pointer to double.

    A pointer to a pointer to a double is declared as double **a.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    In general, the type is best understood by reading it from right to left:
    [12] an array of twelve <- * pointers <- double to doubles.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I've come to think of it as kind of a spiral from the middle.
    http://www.codeproject.com/cpp/compl...ight_left_rule
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    So if i have

    Code:
    int [5] = {10,20,30,40,50}
    if i say
    Code:
    (x + 2)
    would that give me 30

    and if i go
    Code:
    (*x + 2)
    will that make x point to the address of 30

    but also how about if it was
    Code:
    *(x + 2)

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by bazzano View Post
    So if i have

    Code:
    int [5] = {10,20,30,40,50}
    if i say
    Code:
    (x + 2)
    would that give me 30

    and if i go
    Code:
    (*x + 2)
    will that make x point to the address of 30

    but also how about if it was
    Code:
    *(x + 2)
    If you have
    Code:
    int x[5] = {10,20,30,40,50}
    then
    Code:
    (x + 2)
    is the address of the int containing 30.

    If you have
    Code:
    (*x + 2)
    that would be a value of 12.

    If you have
    Code:
    *(x + 2)
    that would be the value 30.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Could somone elaborate more please?

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps your compiler could.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
       int x[5] = {10,20,30,40,50};
       int *y = (x + 2);
       int z = (*x + 2);
       int p = *(x + 2);
       printf(" y = &#37;p\n", (void*)y);
       printf("*y = %d\n", *y);
       printf(" z = %d\n",  z);
       printf(" p = %d\n",  p);
       return 0;
    }
    Sometimes you just need to look at something a couple hundred times before it is as clear as can be.

    I know I have. And there are still more than a few things on the list.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Thanks mate

  12. #12
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    pointers

    What i dont get is how you got 12 for Z

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by bazzano View Post
    What i dont get is how you got 12 for Z
    *x is 10
    10 + 2 is 12
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  14. #14
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    So does that int z = (*x+2)

    x is alread pointing to the zeroth element in the array but because its a * its getting the value of x which is 10. Then adding 2 to ten.

    And not incrementing where x is pointing too

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by bazzano View Post
    So does that int z = (*x+2)

    x is alread pointing to the zeroth element in the array but because its a * its getting the value of x which is 10. Then adding 2 to ten.

    And not incrementing where x is pointing too
    yes...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM