what is the difference of passing an array to a function like this

function(int x[]){

}

or

like this(int x*){

}

what is the difference both ways seem to work

they are both wrong,

int x[] is often the same as int* x

void function(int x[])
{

}


int main()
{
    int x[20];
    function(x);

    int* y = malloc(20*sizeof(int));
    function(y);
}

They both are same functionally eventhough the syntax is different.

The compiler decay's function(int x[]) into function(int *x)

When you are passing an array to a function, you are implicity passing the address of it invariably.

There is no pass by value when it comes to a function with array as the parameter.

Consider the following program.

#include <stdio.h>

void func (char s[])
{
    printf ("%s", s); 
}

int main()
{
    char a[]="Hello, World";
    func(a);
    return 0;

}

Here the address of the array is passed to the function func

Incase you have a debugger, you can verify when you step into the function call,
where the variable s will hold the address of the array a.

There is no pass by value when it comes to a function with array as the parameter.

It's the other way around. There's no pass by reference in C, period. When passing an array, you're passing the address as a pointer by value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.