Home > Blockchain >  How to pass &arr to a function in c
How to pass &arr to a function in c

Time:11-02

I simply want to pass an &arr in C language.

For example:

#include <stdio.h>

void test( PARAMETER??? )
{
    return;
}

int main()
{
    int arr[] = {1,2,3,4,5,6,7,8};
    test(&arr);
    return 0;
}

How should I declare the parameter?

As it is of type int (*)[8]

I simply want to pass &arr in C language. I know I can pass arr and length argument, but how can I via this?

CodePudding user response:

As it is of type int (*)[8]

If you want to pass &arr, then that is exactly the type you need to declare the parameter as, eg:

void test(int (*param)[8])
{
    // use param as needed...
}

CodePudding user response:

you can pass array in function with array size. but you need to use sizeof() to calculate the size of array

#include <stdio.h>

void test( int arr[], size_t size_of_array )
{
    return 0;
}

int main()
{
    int arr[] = {1,2,3,4,5,6,7,8};
    size_t size_of_array = sizeof(arr)/sizeof(arr[0]);  /* calculate of array size */
    test(arr, size_of_array );
    return 0;
}

CodePudding user response:

You can pass it directly (not an address) to a function like

void test(int prm[restrict 8]);

It will not allow to pass int arr[7], and will allow to pass int arr[9], but you will not know the array size, all what you know, it has the size at least 8.

CodePudding user response:

You can't pass an array as an argument.

You can pass a pointer to its first element:

#include <stdio.h>

void test( size_t n, int *p ) {       // `p` is a pointer an `int`.
   printf( "%zu\n", sizeof(p) );      // `sizeof( int* )`, 8 for me.
   printf( "%zu\n", sizeof(*p) );     // `sizeof( int )`, 4 for me.
   printf( "%d\n", p[0] );            // 1
   printf( "%d\n", p[1] );            // 2
}

int main( void ) {
   int arr[] = {1,2,3,4,5,6,7,8,9,10,11};
   test( sizeof(arr)/sizeof(*arr), arr );
}

Same thing in disguise:

#include <stdio.h>

void test( size_t n, int p[n] ) {     // `p` is a pointer an `int`.
   printf( "%zu\n", sizeof(p) );      // `sizeof( int* )`, 8 for me.
   printf( "%zu\n", sizeof(*p) );     // `sizeof( int )`, 4 for me.
   printf( "%d\n", p[0] );            // 1
   printf( "%d\n", p[1] );            // 2
}

int main( void ) {
   int arr[] = {1,2,3,4,5,6,7,8,9,10,11};
   test( sizeof(arr)/sizeof(*arr), arr );
}

Finally, you could pass a pointer to the array:

#include <stdio.h>

void test( size_t n, int (*p)[n] ) {  // `p` is a pointer to an `int[11]`
   printf( "%zu\n", sizeof(p) );      // `sizeof( int* )`, 8 for me.
   printf( "%zu\n", sizeof(*p) );     // `sizeof( int[11] )`, 44 for me.
   printf( "%d\n", (*p)[0] );         // 1
   printf( "%d\n", (*p)[1] );         // 2
}

int main( void ) {
   int arr[] = {1,2,3,4,5,6,7,8,9,10,11};
   test( sizeof(arr)/sizeof(*arr), &arr );
}

In any case, you will need the pass the size of the array if you need it, because it's not stored anywhere in the array itself.

  •  Tags:  
  • c
  • Related