Thread: Passing a pointer to a struct

  1. #1
    Registered User Natase's Avatar
    Join Date
    Aug 2001
    Posts
    123

    Question Passing a pointer to a struct

    The book I have on this is completely useless (even it's examples don't work).

    I am trying to pass a pointer to a structure to a function.

    This is the structure:

    Code:
    struct cavestruct {
       int number;
       int door[3];
       bool visited;
    } cave[200];
    how would I pass this (either as a pointer or in its entirety) to another function?

    ie. how would I change this (keeping the values already being passed)...

    Code:
    result=find_path(start_position, 79);
    Maybe I should just leave it as a global variable...

    P.S. To declare a pointer to an array of structures is it necessary to create an array of pointers or can I just declare one pointer to point at the address of the first element?

    Thanks muchly...

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    This may not be 100% but should show you roughly how to go about this.

    typedef struct
    {
    int iSize;
    char sLocation[64];
    }MY_STRUCT;


    MY_STRUCT MyStruct[200];

    //PROTOTYPE
    int FindThat(MY_STRUCT **Data,int iIndex);

    //CALL
    iReturn=FindThat(&MyStruct,ArrayIndex);
    (This should send the whole array)

    //In FindThat() Function declare a pointer to use in locking down the array
    MY_STRUCT *pData;
    //Lock it to the pointer
    pData=(MY_STRUCT*)*Data;

    //use the array like
    pData[iIndex]

  3. #3
    Unregistered
    Guest
    struct cavestruct {
    int number;
    int door[3];
    bool visited;
    } cave;


    typedef cave v;

    then in ur main program u
    int main () {

    v *x;
    x = (v *)malloc(200*sizeof(v));

    Now u can pass this pointer to an function.
    }

    Now for cave[0]......................................cave[199] u could use the structure by incrementing the pointer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to struct
    By Paul Johnston in forum C Programming
    Replies: 4
    Last Post: 06-11-2009, 03:01 AM
  2. traverse struct using pointer calculus
    By jeanluca in forum C Programming
    Replies: 15
    Last Post: 06-03-2009, 02:18 AM
  3. struct pointer
    By t014y in forum C Programming
    Replies: 5
    Last Post: 01-26-2009, 03:50 PM
  4. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  5. Replies: 1
    Last Post: 03-24-2008, 10:16 AM