pointers

This is a discussion on pointers within the C Programming forums, part of the General Programming Boards category; Hello, this is a problem from a written homework assignment: i have to draw out a stack model for this ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    13

    pointers

    Hello, this is a problem from a written homework assignment:
    i have to draw out a stack model for this


    Code:
    void foo(int **i, int **j){
    int *temp;
    
    if (**i < **j) {
        temp = *i;
        *i = *j;
         *j = temp;
        }
    }
    
    int main() {
    
    int a = 3;
    int b = 4;
    int *x, *y;
    x = &a;
    y = &b;
    
    foo(&x, &y);
    
    printf("a = %d\n", a);
    printf("b = %d\n", b);
    
    printf("*x = %d\n", *x);
    printf("*y = %d\n", *y);
    As a hint, he gave us the output:
    --output--
    a=3
    b=4
    *x = 4
    *y = 3

    I have a = 3, b = 4 and x points to a(3) and y points to b(4). however, when foo is called, i dont understand the double pointers. Somehow, judging by the output, the x and y pointers are switched using i and j. any help would be appreicated

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Double pointer is a variable like any other. What is significant is that it's store an adress of pointer variable.
    for example if you want to swap two integer variables with function you need to use two pointer to type int ( pass by reference). What if you want to swap two pointers? You just need to to use two double pointer to type int and so on.
    Key is to think about pointer variable and also double pointer like like an ordinary variable.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    13
    hmm ok, but does i point to x or does i point to what x points to?

  4. #4
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Ok this is basically your assigment chain
    Code:
    a = 3;
    x = &a;
    i = &x;
    So we can see that a is equal to 3.
    We can see that x points to a.
    We can see that i points to x which points to a.

    So if I had x and wanted the value stored by a, I would dereference it once, aka
    Code:
    *x = 3;
    So if I had i and wanted to get what x was I would dereference it once, aka
    Code:
    *i = x;
    However we know that x points to a, so to get to a from i we would need to dereference it twice, aka
    Code:
    **i = a = 3;
    edit - You could look at it like this, we know:
    Code:
    *x = a
    *i = x
    So some simple substitution:
    Code:
    *(*i) = a;
    Which is where we wound up above: **i = a;
    Last edited by andyhunter; 02-13-2005 at 04:08 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    13
    ok thanks

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, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21