#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    Pointers and variables


    how are pointers and variables different?

    I encountered pointers in a chapter of my textbook on functions with multiple outputs.
    it said that in the function
    Code:
    double separate (double num, char *signp, int *wholep)
    signp and wholep are pointers.
    the textbook explained that pointers store addresses of other memory cells.

    so if the function call is
    Code:
    separate (value, &sn, &whl);
    1. function takes value and computes output
    2. output is stored in some cell, say x
    3. *signp stores the address of x
    4. *signp "points" to the variable sn where the actual data is stored
    5. sn goes to the address and stores the data (not the adress)

    is this how it works?

    also, I notice that in the function calls, there's an ampersand before output variables just like when you store variables from the scanf function.
    what is the effect of placing an ampersand before a variable?
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    Yes, you seem to understand correctly, though be careful about that asterix (*).

    A variable contains a value, while a pointer contains the location of a value.

    You declare a pointer using an asterix; eg,
    char *signp;

    You also use an asterix to dereference the value that the pointer points to; eg,
    char ch = *signp; // assign the char value at signp to the variable ch
    *signp = 'm'; // store 'm' to the location pointed to by signp

    Every variable is stored somewhere, so it has a location. You can store the location of a variable in a pointer using the address operation, & . That is the ampersand that you are talking about.

    When you call a function, all you can pass to it are copies of the arguments, so if you want to change an argument then you have to pass its address so that the function can dereference that address to change those variables. For example, swapping the values in two variables:
    Code:
    void swap(int *a, int *b)
    {
        int temp;
    
        temp = *a;
        *a = *b;
        *b = temp;
    }
    You would call that with
    swap(&x, &y);

    There's nothing magic about it. Every variable has a memory location. All the pointer contains is a memory address. You just need to understand the notation that is being used.

    Oh, and also! The name of an array acts the same as a pointer. So for example:
    Code:
    char *strcpy(char *dest, const char *src);
    
        char s[10];
        strcpy(s, "example");
    No need for any ampersand, since s is already a pointer.
    Last edited by dwise1_aol; June 21st, 2013 at 08:08 PM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    thank you.

    so I guess in a function, the pointer acts like a nickname for the argument,
    because when you say *a in the swap function, you actually mean x.

    The name of an array acts the same as a pointer.
    I haven't studied arrays yet, but after I do, I'll keep that in mind when I do.

IMN logo majestic logo threadwatch logo seochat tools logo