Pointers, Arrays, and function returns
Author |
Message |
eat4 #1 / 8
|
 Pointers, Arrays, and function returns
Hello, I am learning to program in c and have just encountered arrays, and pointers, neither of which i understand very well. However I have read here that functions cannot return arrays, but can return pointers. Can someone pease walk me through a basic array return proceedure? the code below is represenitive of what i would like to do. thank you. # include <stdio.h> /*an array*/ subfunction(void); int main(void); { float values[10]; values = subfunction(); exit(0): Quote: }
/*an array*/subfunction(void) { float x; float subarray[10]; for(x=0;x<10;x++); { subarray[x] = x; Quote: }
return(/*an array*/); Quote: }
|
Fri, 06 May 2005 04:45:21 GMT |
|
 |
Mike Wahle #2 / 8
|
 Pointers, Arrays, and function returns
Quote: > Hello, I am learning to program in c and have just encountered arrays, > and pointers, neither of which i understand very well. However I have > read here that functions cannot return arrays, but can return > pointers. Can someone pease walk me through a basic array return > proceedure? the code below is represenitive of what i would like to > do. > thank you. > # include <stdio.h>
/* Also use: */ #include <stdlib.h> Quote: > /*an array*/ subfunction(void);
/* replace above line with: */ void subfunction(float *, size_t); Quote: > int main(void);
/* replace above line with: */ int main void() /* no semicolon */ Quote: > { > float values[10];
/* for safety, initialize your variables when you define them (not mandatory, but imo a good idea */ float values[10] = {0}; Quote: > values = subfunction();
/* replace above line with: */ subfunction(values, sizeof values / sizeof *values); Quote: > exit(0):
/* replace above line with: */ return 0; Quote: > } > /*an array*/subfunction(void)
/* replace above line with: */ void subfunction(float *subarray, size_t elements) Quote: > { > float x;
/* replace above line with: */ size_t x = 0; Quote: > float subarray[10];
/* remove above line */ Quote: > for(x=0;x<10;x++);
/* replace above line with: */ for(x = 0; x < elements; x++) Quote: > { > subarray[x] = x; > } > return(/*an array*/);
/* remove above line */ Quote: > }
You might find this tutorial useful: http://pw1.netcom.com/~tjensen/ptr/pointers.htm HTH, -Mike
|
Fri, 06 May 2005 05:03:45 GMT |
|
 |
Tom St Deni #3 / 8
|
 Pointers, Arrays, and function returns
Quote: > Hello, I am learning to program in c and have just encountered arrays, > and pointers, neither of which i understand very well. However I have > read here that functions cannot return arrays, but can return > pointers. Can someone pease walk me through a basic array return > proceedure? the code below is represenitive of what i would like to > do.
There are two ways to work with this 1) Pass the address of the first element to your function and not actually "return" anything or 2) Allocate ram for a new array, work on it and return the pointer. Quote: > thank you. > # include <stdio.h> > /*an array*/ subfunction(void); > int main(void); > { > float values[10]; > values = subfunction();
change that to subfunction(values) Quote: > exit(0): > } > /*an array*/subfunction(void)
this to void subfunction(float *values)
|
Fri, 06 May 2005 04:58:07 GMT |
|
 |
Emmanuel Delahay #4 / 8
|
 Pointers, Arrays, and function returns
Quote: > Hello, I am learning to program in c and have just encountered arrays, > and pointers, neither of which i understand very well. However I have > read here that functions cannot return arrays, but can return > pointers. Can someone pease walk me through a basic array return
More precisely, a function can return the address of an array, as long as this address is still valid after the function call. Returning the address of a local variable leads to undefined behaviour (aka "a serious bug"!). Quote: > proceedure? the code below is represenitive of what i would like to > do. > thank you. > # include <stdio.h> > /*an array*/ subfunction(void);
Put the type of an element here with a '*' to indicate that the return type is a pointer. Quote: > int main(void); > { > float values[10]; > values = subfunction();
Doesn't work. You can't assign directly an array. You must copy each element one by one, or enclose it into a structure. Quote: > exit(0): > } > /*an array*/subfunction(void) > { > float x; > float subarray[10]; > for(x=0;x<10;x++); > { > subarray[x] = x; > } > return(/*an array*/); > }
No way. You must change your design. Actually, you have 2 options. - pass the address and number of elements to the function. void subfunction (float *p_array, size_t nb_elem) { Quote: }
- let the function dynamically allocate the array, and return the address of it. Then, you will have to free the block after use. float *subfunction (size_t nb_elem /* (I bet you'll need it...) */) { ... *p = malloc(...); ... return p; Quote: }
Well, there is a third options with a structure enclosing an array, but it only works fine with small arrays (say 10 to 20 elements max) because it involves a lot of memory copies. It can be too slow... -- -ed- emdel at noos.fr ~]=[o FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/ C-library: http://www.dinkumware.com/htm_cl/index.html "Mal nommer les choses c'est ajouter du malheur au monde." -- Albert Camus.
|
Fri, 06 May 2005 05:25:56 GMT |
|
 |
Mac #5 / 8
|
 Pointers, Arrays, and function returns
Quote:
> Hello, I am learning to program in c and have just encountered arrays, > and pointers, neither of which i understand very well. However I have > read here that functions cannot return arrays, but can return > pointers. Can someone pease walk me through a basic array return > proceedure? the code below is represenitive of what i would like to > do. > thank you. > # include <stdio.h> > /*an array*/ subfunction(void); > int main(void); > { > float values[10]; > values = subfunction(); > exit(0): > } > /*an array*/subfunction(void) > { > float x; > float subarray[10]; > for(x=0;x<10;x++); > { > subarray[x] = x; > } > return(/*an array*/); > }
You should read the FAQ list for comp.lang.c (CLC). But here is a way to do what you want, as near as I can tell. This is based on your example. #include <stdlib.h> #include <stdio.h> float *subfunction(int n); int main(void) { float *values; int i; values = subfunction(10); if (values == NULL) { puts("subfunction returned NULL!\n"); return EXIT_FAILURE; } for (i=0; i<10; i++) printf("values[%d] = %f\n", i, (double)values[i]); free(values); return EXIT_SUCCESS; Quote: }
float *subfunction(int n) { float *x; int i; x = malloc(n * sizeof *x); if (x != NULL) for(i=0;i<n;i++) x[i] = i; return(x); Quote: }
|
Fri, 06 May 2005 14:09:42 GMT |
|
 |
Bertrand Mollinier Touble #6 / 8
|
 Pointers, Arrays, and function returns
Quote:
> > Hello, I am learning to program in c and have just encountered arrays, > > and pointers, neither of which i understand very well. However I have > > read here that functions cannot return arrays, but can return > > pointers. Can someone pease walk me through a basic array return > > proceedure? the code below is represenitive of what i would like to > > do. > > thank you. > > # include <stdio.h> > /* Also use: */ > #include <stdlib.h> > > /*an array*/ subfunction(void); > /* replace above line with: */ > void subfunction(float *, size_t); > > int main(void); > /* replace above line with: */ > int main void() /* no semicolon */
ITYM int main(void) /* no semicolon */ Quote: > <snip>
--- bmt Currently available for employment in the San Francisco Bay Area http://www-eleves.enst-bretagne.fr/~mollinie
|
Fri, 06 May 2005 20:41:26 GMT |
|
 |
Mike Wahle #7 / 8
|
 Pointers, Arrays, and function returns
[snip] Quote: > > > int main(void); > > /* replace above line with: */ > > int main void() /* no semicolon */ > ITYM > int main(void) /* no semicolon */ > > <snip>
Darn fingers. :-) -Mike
|
Sat, 07 May 2005 01:46:43 GMT |
|
 |
Chris Tore #8 / 8
|
 Pointers, Arrays, and function returns
Quote:
>Hello, I am learning to program in c and have just encountered arrays, >and pointers, neither of which i understand very well.
Read the FAQ, and/or see <http://67.40.109.61/torek/c/pa.html>. Key concepts here include: - "lvalue vs rvalue", or "object vs value" as I prefer to call it. - Arrays are at best second-class citizens in C. They lack the rights and privileges of simpler data types like "int" and "double", or even those of "struct"s. Most of the time, when you think you have hold of an array, it oozes out of your grasp and leaves you with just a pointer instead. - A pointer to a single element within any array is "just as good" as a pointer to the entire array. -- In-Real-Life: Chris Torek, Wind River Systems (BSD engineering)
http://67.40.109.61/torek/ (for the moment) (you probably cannot email me -- spam has effectively killed email)
|
Sat, 07 May 2005 03:33:11 GMT |
|
|
|