Home > Blockchain >  why do we use pointers to declare an array as a function parameter? (in c)
why do we use pointers to declare an array as a function parameter? (in c)

Time:11-06

a beginner question over here but why exactly do we do that? like how does that work/what's the logic behind it?

example of what I mean (I'm also wondering why we only did that with the Create function)

#include<stdio.h>
void create(int *T,int n){  int i; printf("tab: ");
    for(i=0;i<n;i  ) scanf("%d",&T[i]);
}

void print(int T[],int n){ int i;
    for(i=0;i<n;i  ) printf("[%d]",T[i]);
}

int main(){ int n;
printf("size: "); scanf("%d",&n);
int T[100];
create(T,n);
print(T,n);
}

CodePudding user response:

Unless it is the operand of the sizeof, _Alignof, or unary & operators, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T" will be converted, or "decay", to an expression of type "pointer to T" and its value will be the address of the first element of the array.

So when you call a function with an array expression as a parameter:

T arr[N];
...
foo( arr );

what the function actually receives is a pointer to the first element of the array - it's equivalent to calling the function as

foo( &arr[0] );

There is a reason for this behavior. Ritchie derived C from an earlier language called B, and when you created an array in B the compiler set aside an extra word to store an offset to the first element. The subscript operation a[i] was defined as *(a i) - given the starting address stored in a, offset i words from that address and dereference the result.

Ritchie wanted to keep that subscript behavior (a[i] == *(a i)), but he didn't want to keep the explicit pointer that behavior required, so instead he created the rule that any time the compiler saw an array expression it would convert that expression to a pointer to the first element, except in the cases listed above.

No more need to store a separate pointer, but now arrays lose their "array-ness" under most circumstances, including when they are passed as function arguments.

In the context of a function parameter declaration, T a[N] and T a[] are "adjusted" to T *a. Most of the time we just declare it as a pointer, since that's what we actually receive.

CodePudding user response:

int T[] is the same as int* T in a function parameter. Arrays always decay to pointers in C when passed to functions. So int T[] and int* T are different syntax for the same thing, and you can use either or.

CodePudding user response:

A pointer is just a memory address. When we say int* a, it means "a memory address where we expect an element of type int to be stored". Arrays are internally treated as pointers. Lets look at the following code so that you can understand this better.

int b[4] = {1,2,3,4};

*b == 1, because b is the memory address where the first element of the array is stored.

What happens when we access b[1]? Well, the compiler knows that each element of the array is of type int, and it knows the size of type int. So it just adds that to the original memory address of the b pointer and returns the content of that address.

Of course, you can replicate this behavior without the use of arrays. For example:

*(b) = b[0]
*(b 1) = b[1]
*(b 2) = b[2]

and so on.

Hope this helps. I recommend you to start tinkering with this until you really understand it: pointer arithmetic is very important to understand C programming.

  • Related