How do I pass an array as a a parameter in a function?
I don't want to pass the reference/pointer to the array.
Thanks.
How do I pass an array as a a parameter in a function?
I don't want to pass the reference/pointer to the array.
Thanks.
Wrap the array in a struct type, then pass such a struct to the function as an argument.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Why not? That is the way to do it easily.
#include <stdio.h>
Code:void printarr(int a[]) { a[0] = 9; printf(" %d\n",a[0]); } main() { int a[5]; int i; a[0] = 2; printarr(a); printf("a: %d\n",a[0]); }
That's passing a pointer
Yes, and generally you do want to do that. Even if you take my suggestion, you probably should then pass a pointer to the struct, precisely to avoid possibly expensive copying.Originally Posted by jordanguyoflove
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
you can also pass individual elements of the array to a function but changes made in the function will not be visible by the calling function.heres an example
Code:int main() { int a[]={10,20,30} for(i=0;i<=2;i++) display(a[i]); } void display(int m) { printf("%d",m); }
Last edited by BEN10; 03-31-2009 at 10:19 AM.