0

Why an int* array can't be initialized directly but char* array can be? I know they are not same but what's the exact difference that causes this to happen (please explain why)?

I know why it's not possible for int* p[] to be initialized directly.
I don't know how it's possible for char* name[]?

For example:

char* name[] = { "Mostafa Chamran", "Mehdi Zeinoddin", "Ebrahim Hemmat" };

and

int* p[3] = {1, 2, 3};

give error: invalid conversion

I didn't know what to search to find a related question so I don't know whether the same question exists or not

Regent
  • 5,142
  • 3
  • 21
  • 35
user3783574
  • 127
  • 9
  • because `"string literal like this"` is a `const char *` but numeric constants are not `int *`s – phuclv Sep 13 '14 at 10:01
  • according to T.J. Crowder they are const char (no *) – user3783574 Sep 13 '14 at 10:23
  • `const char[]` is the same as `const char *`. It's only different when declaring variables – phuclv Sep 13 '14 at 10:26
  • did you read all his sentence? How can a `const char` (a character) be cast into `char*` (a pointer)? – phuclv Sep 13 '14 at 10:27
  • 1
    @user3783574: String literals are `const char[x]`, not `const char`. `"foo"` is `const char[4]`, for instance. `const char[z]` is castable to `char*`. – T.J. Crowder Sep 13 '14 at 10:39
  • im not native speaker so do u mean it can be changed to by saying that its castable? or u mean it is const char* – user3783574 Sep 13 '14 at 10:41
  • 2
    @user3783574: `const char[x]` isn't `char *` but can be cast to it implicitly ("implicitly" = "without actually writing something saying 'do this cast'). – T.J. Crowder Sep 13 '14 at 10:43
  • so there is a deference between BEING const char* and being castable to it i gguess? – user3783574 Sep 13 '14 at 10:46
  • @user3783574: Yes. For example, `int` is not `long`, but `int` can be *implicitly cast* to `long`. *(Note: If you don't do `@T.J.`, I don't get notified of your comment.)* – T.J. Crowder Sep 13 '14 at 11:38

2 Answers2

5

The latter expression is an array of pointers. The error indicates that you are converting int to int*.

The expression should be

int arr[3]={1, 2, 3};
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
  • i know that but what about char* name?why it can hold literals? but int* p cant hold numbers ? i mean why char* can hold literals but int* cant hold numbers arent they same like numbers? – user3783574 Sep 13 '14 at 10:07
  • 2
    @user3783574: Because [the type of a string literal is "array of `const char`"](http://stackoverflow.com/a/15508173/157247), which can be implicitly cast to `char*`. – T.J. Crowder Sep 13 '14 at 10:10
4

The entries you're giving for p aren't int* entries, they're int entries, so the type is incorrect (int isn't int*). The string literals you're giving for the name entries are const char [x] (where x is the length of the literal*), which can be implicitly cast to char*, so the types are acceptable and that works.

Since your p is an array of int*, the entries must be int* or implicitly castable to it, just like the entries for your name array of char* are implicitly castable to char*.


(* "foo" is const char[4], for instance — 'f', 'o', 'o', '\0'.)

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875