varunsinghal65
varunsinghal65

Reputation: 556

Explanation of code snippet regarding how to pass an array as a function argument using a pointer?

Following is the CODE SNIPPET to add elements of an array in C language!

Main function :

int main ()
{
    int a[3]={10,11,12}; 
    printf("%d\n" , arraysum(a,3) );
}

Arraysum function definition :

int arraysum (int *addr , int len )
{
    int sum = 0, i ;
    for (i=0 ; i<len ; i++)
        sum += addr[i];
    return sum ;
}

OUTPUT : 33

QUESTIONS : I know that in the main function the base address of the array a is passed to pointer addr , but after that I am unable to understand how following statement is working :

sum+=addr[i];

Q1 : Next , addr was a pointer variable , then how am I using it as an array in arraysum function ?

Q2 : More importantly when I passed base address of array a to the pointer then how come i am able to access all the elements of array a through it ?

Upvotes: 3

Views: 352

Answers (4)

juanchopanza
juanchopanza

Reputation: 227370

Q1: You can index pointers as if they were arrays (in fact, array indexing really is pointer indexing). So

addr[i]

is the same as *(addr + i)

Q2: because when you pass an array to a function expecting a pointer, the array decays to a pointer to its first element. So addr points to the first element of a. This is a simplified version of the same phenomenon, without a function call:

int a[] = {1, 2, 3}; // size 3 array of int
int * p = a;         // OK: a decays to int*. p points to &a[0]
p[0];                // same as a[0]

Note that despite everything, a and p have different types. a is a size 3 array of int. The type contains size information. p is just a pointer to int. It can point to an element of an array, but it can also point to a single int.

Upvotes: 2

Bence Gedai
Bence Gedai

Reputation: 1511

Basically int *addr point to the first element in your array. So if you say addr[i] it equals to *(addr + i), which points to the ith element.

Upvotes: 2

ravi
ravi

Reputation: 10733

When you pass plain array to function, it decays to a pointer. So, we would talk about pointer from now onwards.

If you get a pointer to memory location (ptr) what all things you could do with it:-

de-reference it
     *ptr

increment it 
     ++ptr;

etc...

So, this function doesn't know you have an array. You happen to behave in this function as this pointer is pointer into as array. That means you can increment it to get to the next element of an array as array elements are in consecutive memory location. However, you have to ensure that you don't cross the bound. For that you explicitly pass size of array to this function.

Upvotes: 0

Joseph Mansfield
Joseph Mansfield

Reputation: 110648

Q1 : Next , addr was a pointer variable , then how am i using it as an array in arraysum function ?

Actually, you have this backwards. The subscript operator [] applies to pointers, not to arrays. Huh? What am I on about? Well when you do array[i] where array is an array, the array actually decays to a pointer to its first element. So you are always applying [] to a pointer. In your case, addr is already a pointer to its first element.

Q2 : More importantly when i passed base address of array a to the pointer then how come i am able to access all the elements of array a through it ?

When you do something[i], it is equivalent to *((something) + (i)). This is just basic pointer arithmetic. Take the pointer pointing at the first element, increment the pointer by i, then dereference it.

It is because of this pointer arithmetic that we can access all of the elements of an array through a pointer to one of its elements.

Upvotes: 3

Related Questions