Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot convert from 'int *' to 'int []'?

Tags:

c++

c

pointers

I know this might be a common question but I have tried to search but still cannot find a clear answer.

I have the following code:

int* f() {
    int a[] = {1,2,3};
    return a;
}

int main() {

    int a[] = f(); // Error here

    getch();
    return 0;
}

This code produces the error message: "Cannot convert from 'int *' to 'int []'"

I found this quite strange because I have read that pointer and array are similar. For example, we can use a[i] instead of *(a + i). Can anyone give me a clear explanation, please?

like image 829
ipkiss Avatar asked Jun 27 '11 12:06

ipkiss


People also ask

How do I convert a string to a number in C#?

Using the Parse Method Parse() method to convert the length from string to int (32-bit signed integer equivalent).

What is the use of INT in C#?

int is a keyword that is used to declare a variable which can store an integral type of value (signed integer) the range from -2,147,483,648 to 2,147,483,647. It is an alias of System. Int32.


2 Answers

There are actually two errors in this code.

Firstly, you are returning the address of a temporary (the int array within f), so its contents are undefined after the function returns. Any attempt to access the memory pointed to by the returned pointer will cause undefined behaviour.

Secondly, there is no implicit conversion from pointers to array types in C++. They are similar, but not identical. Arrays can decay to pointers, but it doesn't work the other way round as information is lost on the way - a pointer just represents a memory address, while an array represents the address of a continuous region, typically with a particular size. Also you can't assign to arrays.

For example, we can use a[i] instead of *(a + i)

This, however, has little to do with the differences between arrays and pointers, it's just a syntactic rule for pointer types. As arrays decay to pointers, it works for arrays as well.

like image 170
Alexander Gessler Avatar answered Oct 20 '22 04:10

Alexander Gessler


The type int[] doesn't actually exist.

When you define and initialize an array like

int a[] = {1,2,3};

the compiler counts the elements in the initializer and creates an array of the right size; in that case, it magically becomes:

int a[3] = {1,2,3};

int[] used as a parameter to a function, instead, it's just plain int *, i.e. a pointer to the first element of the array. No other information is carried with it, in particular nothing about the size is preserved. The same holds when you return a pointer

Notice that an array is not a pointer: a pointer can be changed to point to other stuff, while an array refers always to the same memory; a pointer does not know anything about how big is the space of memory it points to, while the size of an array is always known at compile time. The confusion arises from the fact that an array decays to a pointer to its first element in many circumstances, and passing it to a function/returning it from a function are some of these circumstances.

So, why doesn't your code work? There are two big errors:

  1. You are trying to initialize an array with a pointer. We said that an int * doesn't carry any information about the size of the array. It's just a pointer to the first element. So the compiler cannot know how big a should be made to accomodate the stuff returned by f().

  2. In f you are returning a pointer to a variable that is local to that function. This is wrong, because a pointer does not actually store the data, it only points to where the data is stored, i.e. in your case to the a local to f. Because that array is local to the function, it ceases to exist when the function exits (i.e. at the return).

    This means that the pointer you are returning points to stuff that does not exist anymore; consider the code:

    int * a = f();
    

    This initialization works, and you can try to use a later in the function, but a will be pointing to the no-longer existent array of f; in the best case your program will crash (and you'll notice immediately that you've done something wrong), in the worst it will seem to work for some time, and then start giving strange results.

like image 12
Matteo Italia Avatar answered Oct 20 '22 05:10

Matteo Italia