Thread: Pass struct array to function

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    15

    Pass struct array to function

    How am I to pass an array struct to another function?
    Can someone give me an example.
    I do not want a ready-made program. Just some help.
    I do not really follow how to pass an array structure to a function.


    Please help me.
    I have not written any code as yet coz, I don't know how to do it.

    Thanks

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Arrays are usually passed by pointers. Turn your array of structs into a linked list, where each node has a pointer to the next node. Then you just pass the function a pointer to the first node.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Or you can just pass the array name into the function, because arrays are passed by pointer and you don't have to worry about a linked list.

    I guess the question is, have you never passed an array into a function before?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Arrays are actually always passed by pointer, so this is not so hard. If your declaration looks like this:
    Code:
    struct example ray[3];
    You can use either of these in a function prototype:
    Code:
    void testfunc (struct example *ray);
    void testfunc (struct example ray[3]);
    Here's a little demo:
    Code:
    #include <stdio.h>
    
    struct example {
    	char *ptr;
    	int ID;
    };
    
    /* this needs to be global since testfunc does not copy, it just links */
    char data[3][8]={"one", "two", "three"};
    
    void testfunc (int size, struct example ray[size]) {  
    	int i, x=0;
    	for (i=0;i<size;i++) {	/* populate the array */
    		ray[i].ptr=data[x]; x++;
    		ray[i].ID=i+1;
    		if (x>2) x=0; } 
    }
    
    int main() {
    	int num = 6, i;
    	struct example ray[num];
    	testfunc(num,ray);
    	/* now back in main... */
    	for (i=0;i<num;i++) printf("%d %s\n",ray[i].ID,ray[i].ptr);	
    	return 0;
    }
    
    Output:
    1 one
    2 two
    3 three
    4 one
    5 two
    6 three
    
    Again, testfunc could have used *ray instead of ray[size].
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    15
    Yes I have never passed an array to a function

    Anyway thanks for all the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Pass struct to a function.
    By Hybird in forum C++ Programming
    Replies: 5
    Last Post: 11-27-2005, 12:12 AM