Reputation: 51
I am getting an error in the return line. "Incompatible pointer to integer conversion returning 'int (*)[2]' from a function with result type 'int'" can someone explain what is theory behind this error and how to fix it? still I am getting right result, but don't know about this error. (hints: I am very beginner ) here is my testing code.
#include <stdio.h>
#include<string.h>
/* two diminsanl array testing*/
int array_function(int phase,int array[][2]);
int main()
{
int phase =1;
int array[][2]={};
array_function(phase, array);
phase =2;
array_function(phase, array);
return 0;
}
int array_function(int phase, int array[][2])
{
if(phase==1)
{
array[0][0]=1;
array[0][1]=2;
array[1][0]=3;
array[1][1]=4;
}
if(phase==2)
{
printf("%d\n",array[0][0]);
}
return array; //<------------- error line
}
Upvotes: 1
Views: 841
Reputation: 123596
So, first of all, the type of the expression array
in array_function
is int (*)[2]
, or "pointer to 2-element array of int
".
Except when it is the operand of the sizeof
or unary &
operator, or is a string literal being used to initialize another array in a declaration, an expression of type "N-element array of T
" will be converted ("decay") to an expression of type "pointer to T
", and the value of the expression will be the address of the first element of the array.
When you call phase_array
from main
, like so:
array_function(phase, array);
the expression array
has type "2-element array of 2-element array of int
"; since it is not the operand of the sizeof
or unary &
operators, it is converted to an expression of type "pointer to 2-element array of int
", and its value is set to the address of the first element (&array[0]
). This pointer value is what gets passed to array_function
.
In the context of a function parameter list, declarations of the forms T a[N]
and T a[]
will be interpreted as T *a
; all three declare a
as a pointer to T
. So,
int array_function(int phase, int array[][2])
is the same as
int array_function(int phase, int (*array)[2])
Which brings us to your error message: you've declared array_function
to return an int
(which you don't ever use in your main
function, btw), but the type of the expression array
is int (*)[2]
; hence the error message. The two types are not *compatible", and the compiler flags this as an error.
Since you've passed the address of the first element of array
to array_function
, any changes you make to the array contents will be reflected in main
, so you really don't need to return the array. As others have suggested, just change the type of the function to void
and don't return anything.
Upvotes: 0
Reputation: 23236
First this line, is not a legal assignment:
int array[][2]={};
For what you are doing, this line would work:
int array[][2]={0,0,0,0};
second As others have pointed out, this line is attempting to return int[][]
. Two problems with this, 1) C cannot return the value representation of an array of ints, (although it can return pointers, such as int **
, you don't need to here. See note at bottom). And 2) the prototype clearly calls for int
return array; //<------------- error line
For now, simply change the line to:
return 0;
Those two changes will result in your code populating array[][]
with no errors.
Note: because you are passing your array by reference, ( array_function(phase, array);
) the values assigned to array within array_function()
are made available back in the main()
function without having them returned as a return
. (i.e. int ** array_function(phase, array)
)
Upvotes: 0
Reputation: 863
If you statically create an array you have to specify both dimensions. Arrays are passed as pointers and if you pass a static array it is good practice to give the dimensions in the parameters of the function. Simply pass the arrays name to any function even if you allocated it dynamically. If you want to return an array you should return an int* or int** for a 2D array.
Let's say you want to create an array in a function and let the caller have it.
int *createArray(int size)
{
int *array = malloc(size * sizeof(int));
return array;
}
You can call this function from your main.
int *list;
list = createArray(2);
And modify list like any other array.
This is your code properly.
#include <stdio.h>
/* two diminsanl array testing*/
void array_function(int phase, int array[2][2]);
int main()
{
int phase = 1;
int array[2][2];
array_function(phase, array);
phase = 2;
array_function(phase, array);
return 0;
}
void array_function(int phase, int array[2][2])
{
if (phase == 1)
{
array[0][0] = 1;
array[0][1] = 2;
array[1][0] = 3;
array[1][1] = 4;
}
if (phase == 2)
{
printf("%d\n",array[0][0]);
}
}
Upvotes: 0
Reputation: 26027
You have int
as return type of function ( int
array_function() ) but are trying to return a pointer(return array;
). Since it look that you don't need to return anything just have the return type as void
or return 0
. Since array is passed by reference it automatically gets the changes that you do to it inside the function.
Upvotes: 0
Reputation: 5612
Well, first of all, your function is expecting an int
return, but you're trying to return an int[][]
.
And, you can't return an array by value in c. You'll need to return a pointer, or wrap the array as part of a struct
.
Upvotes: 1