0

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Shiv
  • 13
  • 2
  • 1
    I doubt that you need to modify the pointer in that function, so I don't see why you would pass `&arr` here and not just `arr`. – Cheatah Nov 02 '22 at 03:56
  • In C, arrays are usually passed to functions by passing a pointer to the first element of the array, not a pointer to the entire array. That way, you have a pointer of type `int *` instead of the awkward pointer type `int (*)[8]`. If you pass `arr` instead of `&arr`, then you are effectively passing `&arr[0]`, because `arr` [decays](https://stackoverflow.com/q/1461432/12149471) to `&arr[0]`. – Andreas Wenzel Nov 02 '22 at 04:12
  • @Cheatah There isn't really a pointer to modify. This is a scheme in C to make the interface to the fcn enforce an array of a fixed, compile-time known length. It has been mentioned in the past in stack overflow posts. – Avi Berger Nov 02 '22 at 04:12
  • The answer is already here: https://stackoverflow.com/a/74284182/4386427 but I can't help wonder why you want to do that. What is the use case for this? – Support Ukraine Nov 02 '22 at 06:33

4 Answers4

3

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...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

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;
}
user8811698
  • 118
  • 1
  • 7
  • 1
    Although this is not what was asked, I agree that this is probably what OP actually needed. – Cheatah Nov 02 '22 at 04:11
  • as per comment from @Cheetah, I edited data type from `int` to `size_t` for `size_of_array`. since `size_t` which is the unsigned integer. so it is guaranteed to hold for array size/index. Thanks for pointing out – user8811698 Nov 02 '22 at 04:36
0

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.

ikegami
  • 367,544
  • 15
  • 269
  • 518
-1

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.

273K
  • 29,503
  • 10
  • 41
  • 64
  • `restrict` says, roughly, the pointer will not be used to access data that is also access through a separate pointer. It has nothing to do with the size of the array. `int prm[static 8]` asserts the resulting pointer parameter is supposed to point to at least 8 elements. That is supposed to allow some optimization of code in the called function. As for its effect on calling code, it is [unenforced or weakly enforced by compilers](https://godbolt.org/z/dc4aj1TT8). – Eric Postpischil Nov 02 '22 at 12:32