Leth Leth - 4 months ago 8
C Question

How can we set the parameter of an function as a pointer array, then call it by giving an array?

The structure should be like this:

int main()
{
int mainArray[12];
func(10,mainArray);

.
.
.
}

void func(int i,int *pointerArray[12])
{
*pointerArray[4]=i;
}


So when I printed the 3th element of
mainArray
in
main
function, it must give me 10.

How can I do that? What is the correct syntax?

Answer

You need to define the function like this:

void func(int i, int pointerArray[12])
{
    pointerArray[2]=i;
}

The type of the pointerArray parameter needs to match the type of what was put in, namely mainArray. That type is int [].

In C, array indexes start at 0. That means the third element has index 2. So that's the index you use to set the third element.