char [length] initialization and dealing with it

前端 未结 5 1384
长情又很酷
长情又很酷 2021-01-28 08:42

I have defined a char array:

char d[6];

Correct me if I\'m wrong regarding following:

At this moment no memory is allocated for variabl

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-28 09:30

    Your code will not compile (gcc 4.6.3) if you do

     char d[6];
     d = "aaaaa";
    

    you will need to do

     char d[6] = "aaaaa" 
    

    to initialise it this way. This is a local variable created on the stack and so in terms of memory issues all you need worry about is not writing/reading beyond the array bounds.

提交回复
热议问题