2

Greetings, I am trying to learn pointers in C, I simply want my "addtwo" function to add 2 to every element of the input integer array, yet I get odd compilation errors, here is the non-pointer version which indeed won't properly compile.

addtwo(int *arr[]) {
    int i=0;
    for(;i< sizeof(arr)/sizeof(int);i++) {
        arr[i] = arr[i] + 2;
    }
}

main() {
    int myarray[] = {1,2,3,4};
    addtwo(myarray);
}

Regards

Zifre
  • 26,504
  • 11
  • 85
  • 105

7 Answers7

7

You've some problems. First, you try to pass a int* to a parameter that's type int**. That won't work. Give it type int*:

void addtwo(int *arr){
    int i=0;
    for(;i< sizeof(arr)/sizeof(int);i++){
        arr[i] = arr[i] + 2;
    }
}

Then, you need to pass the size in an additional argument. The problem is, that when you pass arrays, you really pass just a pointer (the compiler will make up a temporary pointer that points to the array's first element). So you need to keep track of the size yourself:

void addtwo(int *arr, int size){
    int i=0;
    for(;i<size;i++){
        arr[i] = arr[i] + 2;
    }
}


int main(void) {
    int myarray[] = {1,2,3,4};
    addtwo(myarray, sizeof myarray / sizeof myarray[0]);
}

Now it will work. Also put the return type before them. Some compilers may reject your code, since it doesn't comply to the most recent C Standard anymore, and has long been deprecated (omitting the return type was the way you coded with the old K&R C).

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Alternatively, pass the array as an int[4] instead of int*. – Adam Rosenfield Apr 23 '09 at 22:22
  • You cannot do that in C :( the best match for that would be to pass a pointer to the array. then it would look like void addtwo(int (*arr)[4]) ... addtwo(&myarray) . but you could not pass any other size, and it looks rather scary :) – Johannes Schaub - litb Apr 23 '09 at 22:43
  • Have a look on this answer for details: http://stackoverflow.com/questions/779910/should-i-use-char-argv-or-char-argv-in-c/780147#780147 – Johannes Schaub - litb Apr 23 '09 at 22:45
  • Oops, you're right, although saying that you "cannot" do that isn't quite true -- you can still declare the parameter as int[4], but it's no different from int[] or int*. – Adam Rosenfield Apr 24 '09 at 00:15
  • indeed :) i meant to say you cannot pass it as an int[4] . Of course nothing stops you from declaring the parameter as an int[4] :) – Johannes Schaub - litb Apr 24 '09 at 08:44
3

addtwo(int *arr[]) should be addtwo(int *arr)

You cannot use sizeof to get the size of an array from a pointer. Typically you would either pass the size of the array as a separate arg or have some special value marking the last element.

Martin Beckett
  • 94,801
  • 28
  • 188
  • 263
3

Not to do with the compile error, but...

You have to pass sizeof(arr) to the function instead of calling it in the function. When an array is passed to a function, C no longer sees it as an array, but as a single pointer to memory, so that sizeof(arr) as you are calling it now, will return the size of the pointer arr, which is most likely 4.

Here's what I mean in code:

void addtwo(int *arr, int size){
    int i=0;
    for(;i< size;i++){
        arr[i] = arr[i] + 2;
    }
}

int main(){
    int myarray[] = {1,2,3,4};
    addtwo(myarray, sizeof(arr)/sizeof(int));
    return 0; 
}
DeadHead
  • 2,251
  • 16
  • 16
  • 1
    +1 It was correct initially. Post the edit, main is missing a return in the function signature line. The actual return 0; can be dropped though. – dirkgently Apr 23 '09 at 22:20
1

In C a notation int *arr[] is the same as int** arr.

Adam
  • 756
  • 4
  • 10
  • 23
0

You need to pass a pointer to the first element of the array and the array size. Array types decay to pointers in the context of function parameters. Try:

void addtwo(int *arr, size_t size){
    for(size_t i = 0; i < size; i++){
        arr[i] = arr[i] + 2;
    }
}


int main() {
    int v[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    addtwo(v, sizeof v / sizeof v[ 0 ]);
    return 0;
}
dirkgently
  • 108,024
  • 16
  • 131
  • 187
0

Though others already gave the correct response, basically you have an array of pointers when you have

int *arr[]

I doubt that is what you want. If you have

int arr[]

then that will also be equivalent to

int *arr
James Black
  • 41,583
  • 10
  • 86
  • 166
0

addtwo argument declaration really reads:

arr is an array of pointers to integer

when you probably really want

a pointer to an array of integers

"How to Read C Declarations" has really helped me to grok the topic a while ago, maybe it will do the same for you.

Vlad Gudim
  • 23,397
  • 16
  • 69
  • 92