Thread: return an array of structs

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can not return an array from a function, no matter what.

    You can pass in an array and fill it in, or you can return a pointer, or pass in a pointer to pointer and allocate space (and either pass in a pointer to an itneger that gives you the number of items, or return the number of items if you pass a pointer to pointer).

    --
    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.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    You can not return an array from a function, no matter what.

    You can pass in an array and fill it in, or you can return a pointer, or pass in a pointer to pointer and allocate space (and either pass in a pointer to an itneger that gives you the number of items, or return the number of items if you pass a pointer to pointer).

    --
    Mats
    If I pass in an array, I have to declare it's length?
    Code:
    void testfunc (struct *ray[10]) {
    If I return a pointer (eg, struct *ptr (struct *ray) {) should I allocate for the array it points to inside the function, or before I call it?
    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

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Here's an example of the wierdness I'm talking about:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Test {
    	char *name;
    	int age;
    } ;  
    
    void testfunc (struct Test *examp) {
    	if ((examp=realloc(examp,3*sizeof(*examp)))==NULL) puts ("malloc fail");
    	puts("HERE");
    	fflush(stdout);
    	examp[0].name=malloc(5);
    	strcpy(examp[0].name,"five");
    	examp[0].age=5;
    	examp[1].name=malloc(5);
    	strcpy(examp[1].name,"four");
    	examp[1].age=4;
    	examp[2].name=malloc(5);
    	strcpy(examp[2].name,"nine");
    	examp[2].age=9;
    }
    
    int main () {
    	int i=0;
    	struct Test *examp=malloc(sizeof(*examp)); 	 
    	
    	testfunc (examp);
    	printf("sizeof *examp:%d sizeof examp:%d\n",sizeof(*examp),sizeof(examp));
    	printf("%s is %d\n", examp[0].name,examp[0].age);
    	for (i=0;i<3;i++) {puts("X");printf("%s is %d\n", examp[i].name,examp[i].age);}
    	
    }
    Everything is fine. I am surprised that *examp is 8 bytes while examp is 4 bytes, but whatever (???). However, I am a little worried the realloc doesn't really realloc inside the function, because this causes a segfault:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct Test {
    	char *name;
    	int age;
    } ;  
    
    void testfunc (struct Test *examp) {
    	if ((examp=malloc(3*sizeof(*examp)))==NULL) puts ("malloc fail");
    	puts("HERE");
    	fflush(stdout);
    	examp[0].name=malloc(5);
    	strcpy(examp[0].name,"five");
    	examp[0].age=5;
    	examp[1].name=malloc(5);
    	strcpy(examp[1].name,"four");
    	examp[1].age=4;
    	examp[2].name=malloc(5);
    	strcpy(examp[2].name,"nine");
    	examp[2].age=9;
    }
    
    int main () {
    	int i=0;
    	struct Test *examp; 	 
    	
    	testfunc (examp);
    	printf("sizeof *examp:%d sizeof examp:%d\n",sizeof(*examp),sizeof(examp));
    	printf("%s is %d\n", examp[0].name,examp[0].age);
    	for (i=0;i<3;i++) {puts("X");printf("%s is %d\n", examp[i].name,examp[i].age);}
    	
    }
    Why can't I malloc the struct array inside another function and return it?
    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

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    53
    Quote Originally Posted by MK27 View Post
    Everything is fine. I am surprised that *examp is 8 bytes while examp is 4 bytes, but whatever (???).
    examp is a pointer, which (on a 32-bit system) is 4 bytes in size. *examp is what its points to, which in this case is the size of your structure.

    Quote Originally Posted by MK27 View Post
    Why can't I malloc the struct array inside another function and return it?
    You can. The drawback to this technique is that the callers of your function will have to remember to free() the array.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    However, if you want to pass in a pointer, to be filled in inside the function, you should pass a pointer to a pointer - just like if you want to change an integer inside a function, you need to pass a pointer to an integer.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Inserting new member into array of structs
    By cathym in forum C Programming
    Replies: 6
    Last Post: 04-17-2005, 07:10 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM