Passing entire array to the function in C -


i have written program insertion shot following:

int _tmain(int argc, _tchar* argv[]) { int arr[10] = {1,2,3,10,5,9,6,8,7,4}; int value; cin >> value ; int *ptr; ptr = insertionshot(arr); //here im passing whole array binarysearch(arr,value); return 0; }  int * insertionshot(int arr[]) {  //changed after hint (now, fine) int ar[10]; for(int =0;i < 10; i++) {     ar[i] = arr[i]; }  //changed after hint  int arrlength = sizeof(ar)/sizeof(ar[0]); //here array length 1, should 10 for(int = 1; <= arrlength -1 ;a++) {     int b = a;     while(b > 0 && ar[b] < ar[b-1])     {         int temp;         temp = ar[b-1];         ar[b-1] = ar[b];         ar[b] = temp;         b--;     } } return ar;  } 

the problem after passing whole array function, function definition shows 1 element in array , "arraylength" giving 1.

int arr[] in function formal parameter list syntax quirk, processed int *arr. sizeof trick doesn't behave expect.

in c not possible pass arrays value; , furthermore, @ runtime array not remember length.

you include length information passing pointer whole array @ compile time:

int * insertionshot(int (*arr)[10]) 

of course, approach can ever pass array of length 10. if intend able pass arrays of differing length, have pass length parameter.


Comments

Popular posts from this blog

Change the color of an oval at click in Java AWT -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -