What Does Adding One to a Character Array in C Do?

久未见 提交于 2019-12-11 06:25:22

问题


I'm looking through some code for learning purposes. I'm working through this portion of code.

// e.g. const unsigned char data={0x1,0x7C ... }
unsigned char buf[40];
memset(buf,0,40);
buf[0] = 0x52;
memcpy(buf+1, data, length); // What does buf+1 do in this situation?

On the last line where memcpy is called what does buf+1 do? buf is a character array, so what does +1 do to it?


回答1:


In C, every array name is a pointer, so buf here also means the pointer which point to buf[0].Then "buf+1" means "buf[1]"'s address.




回答2:


buf+1 is equivalent to &(buf[1])




回答3:


buf+1 is the same as &(buf[1]). In other words, it returns a pointer to the 2nd (index 1) character of buf.




回答4:


Pointer Arithmetic (Stack Overflow)



来源:https://stackoverflow.com/questions/1391583/what-does-adding-one-to-a-character-array-in-c-do

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