Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A pointer value question

  1. Oct 14, 2008 #1
    when i read about pointers i got the idea that when i want to do a pointer to foo
    int foo=35;

    i do

    Code (Text):

    *foo_ptr=foo;  //*foo_ptr returns the value 35
    &foo_ptr         //has the address of foo
     
    i have a problem in understanding this code
    Code (Text):

    int foo;                         //1st line
    int *foo_ptr = &foo;        //2nd line

    int bar = *foo_ptr;         //3rd line

    *foo_ptr = 42                //4th line
     
    &foo is the address of foo not its value

    *foo_ptr is a pointer to &foo which is the address
    not the variable foo itself

    i think the right way to make a pointer to foo is
    Code (Text):

    *foo_ptr=foo;
     
    and now if we execute *foo_ptr=42

    we get that &foo=42
    the address of foo is now 42

    ???
     
    Last edited: Oct 14, 2008
  2. jcsd
  3. Oct 14, 2008 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    *foo_ptr = 42 sets the value of thing that foo_ptr points to = 42, not the value of &foo

    edit - sorry what are you actually asking?
     
  4. Oct 14, 2008 #3
    i am asking what is the meaning of this line
    Code (Text):

    int *foo_ptr = &foo;
     
     
  5. Oct 14, 2008 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    int *x = y;

    is the same thing as

    int *x;
    x = y;


    Some people prefer to write
    int* x = y;

    to more clearly indicate that it's {type} {variable} = {initializer} however, that isn't advisable because of the quirks with declaring multiple variables on the same line
    int * x, y;
    is the same as
    int * x;
    int y;


    You're probably familiar with array initializers; they work the same way:
    int x[3] = {1, 2, 3};

    And just for the fun of a complicated example...

    double *w = a, x = 0.35, (*y)(double) = &sin, z[3] = {3.0, 2.0, 1.0};
    is the same thing as
    double *w;
    w = a;
    double x;
    x = 0.35;
    double (*y)(double); // y is a pointer to a function
    y = &sin;
    double z[3];
    z[0] = 3.0;
    z[1] = 2.0;
    z[2] = 1.0;
     
    Last edited: Oct 14, 2008
  6. Oct 14, 2008 #5

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    int *foo_ptr = &foo;
    Means make a pointer foo_ptr that points to the address of the memory used by the variable foo.
    So when we change the value of *foo_ptr we changing the contents of the memory referenced by foo.

    Note - you can't change the address of foo after it has been declared on the stack.
     
  7. Oct 14, 2008 #6
    so in that way
    *foo_ptr linked to foo

    when do i know if its declared on a stack?
     
  8. Oct 14, 2008 #7

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    When you define "int foo" you ask the compiler to make room for a integer (eg. 32 or 64bits) on the stack, the stack only exists for the scope of the function.

    When you do "int *foo = new integer" you reserve space on the heap for an integer, and foo will then contain that memory address. The storage is permanent until you delete it or the program ends. You can also change the address and move the memory to somewhere else (within certain limits impossedby the operating system).
     
  9. Oct 14, 2008 #8
    when yo say
    double *w = a;

    i dont know whats "a"

    "a" need to be an address to a variable
     
  10. Oct 14, 2008 #9

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    'a' needs to have already been declared and needs to be the same type as the type of the pointer.

    double a;
    double *w=a;

    a is a real variable stored on the stack, *w is just a shortcut to it.
     
  11. Oct 14, 2008 #10
    it needs to be
    double *w=&a;

    because *w gets the address &a in order to get to the address of the variable "a"

    *w=a is just changing pointing w to some address with the value "a"(not the address of the variable "a")

    ???
     
  12. Oct 14, 2008 #11

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    Yes sorry, should be "double *w = &a;" but the point is the same.
     
  13. Oct 14, 2008 #12
    (*y)(double) = &sin

    whats that ??
    you are casting the word double into a pointer type
    and it doesnt go anywhere

    ???
     
  14. Oct 14, 2008 #13

    KTC

    User Avatar

    Code (Text):
    double (*y)(double);
    is a pointer to a function, the pointer identifier (the name of the pointer variable) is y. It is a pointer to a function that take one parameter, of type double, with a return type of double.

    Since sin from <math.h> has that signutare
    Code (Text):
    double sin( double arg );
    , we can assign its address to the pointer.
    Code (Text):
    y = &sin;
    The & is not necessary in the case of taking the address of a function, which mean one can equally have wrote
    Code (Text):
    y = sin;
     
  15. Oct 14, 2008 #14

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    As KTC explains it's a pointer to a function.
    A pointer to function is an odd feature of 'C' that allows you to pass a function to another function. It's main use is something like sort where the sort() routine doens't know how to compare two values so you have to give it a function to do that.
    Because of the way 'C' uses memory when calling functions it has to know how many and what type of arguements the function has.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: A pointer value question
  1. Pointer question (Replies: 2)

Loading...