Passing array of structures by pointer

This is a discussion on Passing array of structures by pointer within the C Programming forums, part of the General Programming Boards category; Hi there, I have been trying to write a simple program to pass an array of structures via pointer. I'm ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    New Zealand
    Posts
    3

    Passing array of structures by pointer

    Hi there, I have been trying to write a simple program to pass an array of structures via pointer. I'm sorry if this is a trivial question, and I'm sure it has been asked many times before. I have looked on google, however haven't managed to find exactly what may be wrong with my code. Any help would be much appreciated, thanks, Ed

    p.s. I have tried allocating memory using malloc but got even more compile errors..

    Code:
    //Structure Passing example
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct
    {		
     
       int w;
       int h;
    
    } card_t;   //card_t is declared to be of type struct
    
    //prototypes
    void assign(void);
    
    
    card_t *(card_table)[20]; //card_table is a pointer to an array of card_t structures
     
     
    	
    void assign()
    {
    	int w=3;
    	card_table[0].w = w;
    }
    
    
    int main(void)
    {
    	assign();
    	int temp = card_table[0].w;
    	printf("%d",temp);
    	return 0;
    }

    Compile error: "request for member 'w' in something not a structure or union"
    Last edited by Salem; 04-12-2011 at 11:38 PM. Reason: added code tags

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > card_t *(card_table)[20]; //card_table is a pointer to an array of card_t structures
    This would simply be
    Code:
    card_t *card_table;
    Which you would then allocate memory using
    Code:
    card_table = malloc( howManyCardsYouWant * sizeof(*card_table) );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Location
    Bangalore
    Posts
    20
    Allocate memory before assigning the value and card_table[0] is a pointer so, you must use ->
    Code:
    card_table[0]->w = w;

  4. #4
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Welcome to the forums, Ed.

    First, consider the following two variable definitions. Note the location of *.
    Code:
    card_t *(card_table)[20];  // array of 20 pointers-to-card_t
    card_t (*card_table)[20];  // pointer-to-array of 20 card_t
    Next, understand that you're just declaring a pointer. This does not automagically create something for it to point to -- you have to do that yourself. Therefore you would need to do something like this:
    Code:
    card_t table[20];             // declare an array of 20 card_t's
    card_t (*card_table)[20] = &table;  // declare and define pointer-to-array of 20 card_t's
    
    /* use it */
    void assign()
    {
      int w=3;
      (*card_table)[0].w = w;  // dereference card_table to get to table, [0] to access the 1st structure and .w to get the w member of it
    }
    Notice the rather complex syntax for accessing members of a structure in the array. It would be much easier to go with the malloc approaches suggested above.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Location
    New Zealand
    Posts
    3
    Hi guys, thanks so much for the quick replies.

    I have tried the malloc approach as suggested by Salem and also dereferenced card_table in the assign function as suggested by msh

    //Structure Passing example


    #include <stdio.h>
    #include <stdlib.h>

    typedef struct
    {
    int w;
    int h;

    } card_t; //card_t is declared to be of type struct

    //prototypes
    void assign(void);

    card_t *card_table; //card_table is a pointer to an array of card_t structures

    card_table = malloc( 5 * sizeof(*card_t) );


    void assign()
    {
    int w=3;
    (*card_table)[0].w = w;
    }


    int main(void)
    {
    assign();
    int temp = (*card_table)[0].w;
    printf("%d",temp);
    return 0;
    }

    I am now getting the following compile errors relating to the malloc line:

    warning: data definition has no type or storage class
    warning: type defaults to 'int' in declaration of 'card table'
    error: conflicting types for 'card_table'

    Thanks again for your help.
    Ed

  6. #6
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    If you are going the malloc route you don't need to dereference as show in my example. Instead, the following should work for accessing structure members. This is because now there is one less level of indirection.
    Code:
    card_table[0].w = w;  // likewise elsewhere
    You should also check that malloc was able to allocate the requested memory. Check out the documentation for malloc function for hints on how to do that.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    You have to call malloc inside a function.

    And please read about [code][/code] tags for posting code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Location
    New Zealand
    Posts
    3
    Ok, thanks Salem, that was my problem! Would have been handy if my textbook had said that!
    Thanks again everyone for your help,
    Ed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing An Array Of Structures To A Function
    By nexusdarkblue in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 07:24 AM
  2. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 10:01 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. Passing an Array of Structures
    By Explicit in forum C Programming
    Replies: 3
    Last Post: 06-03-2004, 07:29 PM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM

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