Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing pointers of arrays in C

Tags:

arrays

c

pointers

So I have some code that looks like this:

int a[10];
a = arrayGen(a,9);

and the arrayGen function looks like this:

int* arrayGen(int arrAddr[], int maxNum)
{
    int counter=0;
    while(arrAddr[counter] != '\0') {
        arrAddr[counter] = gen(maxNum);
        counter++;
    }
    return arrAddr;
}

Right now the compilier tells me "warning: passing argument 1 of ‘arrayGen’ makes integer from pointer without a cast"

My thinking is that I pass 'a', a pointer to a[0], then since the array is already created I can just fill in values for a[n] until I a[n] == '\0'. I think my error is that arrayGen is written to take in an array, not a pointer to one. If that's true I'm not sure how to proceed, do I write values to addresses until the contents of one address is '\0'?

like image 580
devin Avatar asked Feb 02 '09 02:02

devin


People also ask

Are arrays passed as pointers in C?

Array can be passed to function in C using pointers and because they are passed by reference changes made on an array will also be reflected on the original array outside function scope.

How can we pass array to function in C?

To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.

How do you pass an array to a function by a pointer?

A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name. In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun().

How pointers can be passed to functions in C?

C programming allows passing a pointer to a function. To do so, simply declare the function parameter as a pointer type.


1 Answers

The basic magic here is this identity in C:

*(a+i) == a[i]

Okay, now I'll make this be readable English.

Here's the issue: An array name isn't an lvalue; it can't be assigned to. So the line you have with

a = arrayGen(...)

is the problem. See this example:

int main() {
    int a[10];

    a = arrayGen(a,9);

    return 0;
}

which gives the compilation error:

gcc -o foo foo.c
foo.c: In function 'main':
foo.c:21: error: incompatible types in assignment

Compilation exited abnormally with code 1 at Sun Feb  1 20:05:37

You need to have a pointer, which is an lvalue, to which to assign the results.

This code, for example:

int main() {
    int a[10];
    int * ip;

    /* a = arrayGen(a,9);  */
    ip = a ; /* or &a[0] */
    ip = arrayGen(ip,9);

    return 0;
}

compiles fine:

gcc -o foo foo.c

Compilation finished at Sun Feb  1 20:09:28

Note that because of the identity at top, you can treat ip as an array if you like, as in this code:

int main() {
    int a[10];
    int * ip;
    int ix ;

    /* a = arrayGen(a,9);  */
    ip = a ; /* or &a[0] */
    ip = arrayGen(ip,9);

    for(ix=0; ix < 9; ix++)
        ip[ix] = 42 ;

    return 0;
}

Full example code

Just for completeness here's my full example:

int gen(int max){
    return 42;
}

int* arrayGen(int arrAddr[], int maxNum)
{
    int counter=0;
    while(arrAddr[counter] != '\0') {
        arrAddr[counter] = gen(maxNum);
        counter++;
    }
    return arrAddr;
}

int main() {
    int a[10];
    int * ip;
    int ix ;

    /* a = arrayGen(a,9);  */
    ip = a ; /* or &a[0] */
    ip = arrayGen(ip,9);

    for(ix=0; ix < 9; ix++)
        ip[ix] = 42 ;

    return 0;
}
like image 72
Charlie Martin Avatar answered Oct 19 '22 11:10

Charlie Martin