gcc compile error: cast specifies array type

匿名 (未验证) 提交于 2019-12-03 01:59:02

问题:

The following code is perfect valid,

int *ia = (int[]){1,3,5,7}; 

but when I compile the next line of code,

char *p = (char[]) "abc"; 

gcc says

test.c:87: error: cast specifies array type 

It seems they are casted in the same way. Why did the second one get an err msg?


As you guys said, "abc" is a pointer, which cannot be converted to be a pointer. So my another question: why does

 char[] s = "abc"; 

is valid. How does the above line of code work when compiling?

回答1:

This is valid because the expression on the right hand side is a C99 compound literal, not a cast:

int *ia = (int[]){1,3,5,7}; 

However, this is not valid because it is a cast-expression, not a compound literal. As GCC is telling you, you can't cast to array types:

char *p = (char[]) "abc"; 

You can fix it by making it a proper compound literal - they are denoted by the braces:

char *p = (char[]){"abc"}; 


回答2:

The first example isn't casting it's array creation. And there is huge diffrence between char[] and char* : the first one is the array itself and second one is pointer to array. The following should work (not 100% sure):

char *p = &((char[]) "abc");   

Or

char *p = &((char[]) "abc")[0]; 


回答3:

char *p = (char) {"abc"};

Answer: p is a pointer to the first byte address of the array, not an array type pointer. Any array needs to be initialized with braces {} a char array is initialized like this:

char my_char_arr[3] = {'a','b','c'};
but can be initialized with a string
char my_char_arr[3] = {"abc"};
        char arr[3] = {"abc"}; // a char array with 3 bytes.          char (*ptr)[3]; // a char array type pointer. arr size and ptr size needs to be equal. (= 3)          ptr = arr;  // sets array address to array pointer. 

Now if arr address is 0x10 and its size is 3 bytes then:

        ptr++; 

gives address 0x13. ect, ect.

if you take a multiple dimensions array's the the address's are lined up.

#include  #include   int main(){          int i = 0;          /*   first example. */          char arr[3][9] = { // double dimensional array.                             { "Hello"},                             { "Welcome"},                             { "Good bye."},                             };           char (*ptr)[9];   // array pointer.           ptr = arr;        // assign array to pointer.           for( ; i 

using an array pointer as a typedef example:

 #include   #include   // second example. //  //*** Typedef a array pointer *** //  int i = 0, ERROR = 1, CRASH = 5, GOOD = 6, BUG = 8;  char succes_text[3][60] = {                             {"Awesome performance detected !!\n"},                             {"Your system and program are performing a expected.\n"},                             {"No problems detected, proceeding next task.\n"}                         };  char error_text[3][60] = {                             {"Undefined error detected, call the help-desk.\n"},                             {"Warning, bad algorithmic behavior.\n"},                             {"Program manager found a bug, save your work.\n"}                         };  typedef char (*SUCCES_TEXT_TYPE)[60];  SUCCES_TEXT_TYPE SUCCES_TEXT = succes_text;   typedef char (*ERROR_TEXT_TYPE)[60];  ERROR_TEXT_TYPE ERROR_TEXT = error_text;   char * testfunc(int i, SUCCES_TEXT_TYPE s_txt, ERROR_TEXT_TYPE e_txt){                                             if(i == ERROR){ return (*e_txt);}                                             if(i == CRASH){ e_txt += 1; return (*e_txt);}                                             if(i == BUG){   e_txt += 2; return (*e_txt);}                                             if(i == GOOD){ return (*s_txt);}                                             return "";                                     }       int main(){             for(;i 


回答4:

"abc" can't be cast to a char array since it's not an array to begin with



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!