Assigning a string of characters to a char array

前端 未结 6 884
半阙折子戏
半阙折子戏 2020-12-03 03:14

I Want to know why the first statements works and why not second one in c++

char a[10]=\"iqbal\";  // it works

a=\"iqbal\"; // does not work 
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 03:56

    When writing char a[10]="iqbal" You are initializing the elements of the character array a with the characters. We can do the same with int type (note that the char type gets a slightly different treatment) : int a[10]={1,2,...};

    But writing the following after declaration part would be invalid as a would be treated just like a pointer. So writing something like a={1,2,...}; or a="iqbal" won't be making any sense!

提交回复
热议问题