passing array of pointers from one function to another

This is a discussion on passing array of pointers from one function to another within the C Programming forums, part of the General Programming Boards category; How can I pass an array of pointers? Please help... here's a piece of my code... Code: typedef struct node1 ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    25

    passing array of pointers from one function to another

    How can I pass an array of pointers? Please help... here's a piece of my code...

    Code:
    typedef struct node1 {
    	int cell;
    	struct node *prev;
    	struct node *next;	
    } box;
    
    typedef struct node2 {
    	char name[10];
    	cell *move;
    } player;
    
    void play(box *tail) {
    	player *p[i];
            int n = 5; //...n can be changed by the user..
     //..more codes...
            for(i = 0; i < n; i++) {
    		p[i] = (player *)malloc(sizeof(player));
    		}
            p[i]->move = tail;
    //..more codes...
    
              displayBoard(tail,p[i]->move->cell, *p); //.. I will pass the array of pointers p to displayBoard
    //..more codes...
    }
    
    void displayBoard(box *a, int cell, player *p){ //how can I pass p properly to this function?
    	//..more codes...
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
              displayBoard(tail,p[i]->move->cell, p);
    Perhaps?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Passing the name of the array should do it. Can't pass *p as an array name is not a variable besides being a call by value instead of a call by reference.
    Code:
    void displayBoard(box *a, int cell, player p)

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Well, p is an array of pointers, so this just won't work... without getting a warning or error.
    If you put...
    void displayBoard(box* a, int cell, player* (*p)[i])
    ...then it will work better (tricky type, I know!).
    Also, pass the array with:
    displayBoard(..., &p);
    Basically, it's a pointer to an array of i pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Code:
    displayBoard(..., &p);
    creates a pointer argument with three levels of indirection while
    Code:
    displayBoard(..., p);
    keeps the level of indirection at two.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Try it, it compiles fine for me.
    Other types do not compile fine.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    I am on Unix...what platform are you on??
    displayBoard(..., p) compiles fine for me while displayBoard(..., &p) errors out since the name of an array is not a variable.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Code:
    void foo(int* (* p)[10]) {}
    
    int main()
    {
    	int* n[10];
    	foo(&n);
    }
    I'm using Visual Studio on Windows platform.
    But it's really obvious, the type.
    If we look at the middle, we see it's a pointer to an array (of 10 elements here), and the type of array is the type on the left, which in this case is int*.

    An example with the OP's code:
    Code:
    typedef struct node1 {
    	int cell;
    	struct node *prev;
    	struct node *next;	
    } box;
    
    typedef struct node2 {
    	char name[10];
    	//cell *move;
    } player;
    
    #define i 10
    
    void displayBoard(box* a, int cell, player* (*p)[i]){} //how can I pass p properly to this function?
    
    int main()
    {
    	player* p[i];
    	displayBoard(0, 0, &p); //.. I will pass the array of pointers p to displayBoard
    //..more codes...
    }
    If I use
    p
    instead of
    &p
    I get an error (or warning in C):
    error C2664: 'displayBoard' : cannot convert parameter 3 from 'player *[10]' to 'player *(*)[10]'
    Last edited by Elysia; 10-14-2008 at 12:05 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047

    Cool Clouds have lifted

    Now that you posted how you're defining and passing p to displayBoard() it makes sense. I'm passing p differently from the caller to the callee as in
    Code:
    #define i 10
    
    void displayBoard(box *a, int cell, player *p[]) {}
    
    int main()
    {
         player *p[i];
         displayBoard(0, 0, p);
    }

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,789
    Aha.
    I suppose...
    player* p[]
    ...works too.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Passing a 2d Array of pointers to a Function
    By miclus in forum C Programming
    Replies: 6
    Last Post: 09-11-2004, 07:34 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

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