1
char *x[4] = { "ffg", "fgf", "kkk" "mmm"};
int *x1[4] = { 1, 2 ,3 , 4};

If I create array of character pointers as mentioned above its compiling fine whereas if I create array of integer pointers as mentioned in second line I am getting the error

'initializing' : cannot convert from 'int' to 'int *'

How the syntax is working fine for characters but not working for integer. please explain anyone.

Christian Ternus
  • 8,406
  • 24
  • 39
user369287
  • 692
  • 8
  • 28
  • To add to the confusion, it will "work" if you use a different literal, namely zero, to initialize the array of integer pointers. That's because zero converts to a pointer, too. Did you actually _mean_ to have an array of integer pointers (i.e. what is it you intended to do)? – Damon Oct 23 '13 at 13:59
  • Um, your syntax isn't working fine for characters. The character version is `char *x[4] = { 'f', 'f', 'g' };`, and that has the same problem as the `int` version. – Raymond Chen Oct 23 '13 at 14:08

4 Answers4

10

"ffg" has type char* 1, whereas 1 has type int (not int*). Hope you see the issue now.

1: Well, technically it's char[4] in C, but that implicitly decays into char*. In C++ the type is const char[4] and you should get a warning or an error from the compiler.

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • 4
    I was connvinced `"ffg"` had type `const char[4]` – juanchopanza Oct 23 '13 at 13:46
  • 1
    @juanchopanza: I was sure someone would jump in and correct, but you were super quick. :) Let me think how best to reword it without adding to the confusion. – NPE Oct 23 '13 at 13:47
  • 1
    using [1] for footnoting in a context where [] are being used for array access is unnecessarily confusing - put your note inline or just have a note without footnoting it. – Kate Gregory Oct 23 '13 at 13:52
  • 1
    *Technically* it's `const char [4]`, and this won't even compile under C++11. – John Dibling Oct 23 '13 at 13:52
  • 1
    @JohnDibling: That's the trouble with having questions tagged as both [c] and [c++]. Thanks for that edit BTW. – NPE Oct 23 '13 at 13:55
3

int *x1[4] = { 1, 2 ,3 , 4}; you can't do this , you're storing integers instead of integer pointers , to store integer pointers in your array here's how to procede :

int *x1[4] ;
int i , j , k , l;
x1[0] = &i;
x1[1] = &j;
x1[2] = &k;
x1[3] = &l;
Farouq Jouti
  • 1,657
  • 9
  • 15
2

1, 2 ,3 , 4 are ints not pointers to ints. You might want to do. Casting like this:

int *x1[4] = { (int *)1, (int *)2 ,(int *)3 , (int *)4};

Will make the error go away but de-referencing will cause UB.

int i1=0, i2=0, i3=0, i4=0;
int *x1[4] = { &i1, &i2, &i3, &i4 };

A string literal (like "ffg", "fgf", etc) is kept in a read-only location (implementation dependent) and has type char [].

Since you tagged C++, you can try:

#include <iostream>
#include <typeinfo>

int main()
{
    char arr [5] = "test";
    char *arr1 = "test";
    std::cout << typeid(arr).name() << std::endl;
    std::cout << typeid("test").name() << std::endl;
    std::cout << typeid(arr1).name() << std::endl;
}

Output:

A5_c
A5_c
Pc

As you can see string literal decays to a pointer as said by NPE, although its type is of char[5].

Sadique
  • 22,572
  • 7
  • 65
  • 91
0

There is nothing wrong about an array of pointer. I think you fail at understand what the compiler is telling you.

There is nothing wrong with the syntax. It is the semantic that is not good.

{1, 2, 3, 4} have type int[4] but you are assigning it to an int*[4] and that is not possible.

The error is an initializing error. It tell you that the parser is ok with your code but the semantic analyzer is not for the reason that an int is not the same as int*.

mathk
  • 7,973
  • 6
  • 45
  • 74