0

Recently I started learning c and was experimenting with arrays and passing them between functions.
Now I have written a simple piece of code which has two functions one for generating random numbers and putting them in an array of size 50 and one function to display the contents of the array.

I can pass the array as array[] or *array, what is difference between the two techniques (if there is any) other than not being able to use pointer arithmetic with the former.

Thanks

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by deceptikon
0

There's no difference at all. The array notation for a function parameter is just syntactic sugar. Internally it's treated as a pointer, and you can use it as a pointer, which does indeed include pointer arithmetic:

#include <stdio.h>

void traverse(int a[], int n)
{
    while (--n >= 0)
    {
        printf("%d", *a++);
    }
}

int main(void)
{
    int a[] = {1, 2, 3, 4, 5};
    
    traverse(a, sizeof a / sizeof *a);
    
    return 0;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.