大佬教程收集整理的这篇文章主要介绍了const char数组名是函数中的const值吗?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
void f(char s[]) { s++; // Why it works here? }
但它不是在主要功能时.它连接到我,因为它具有完全相同的数据类型.
void main() { char s[] = "abc"; s++; // wrong because it is a const value. }
这是为什么?
void f(char s[]); // equivalent to void f(char *s);
这意味着s可以包含任何字符的地址.
void f(char s[]) { // s is a pointer to a character. // s++ is fine. evaluates to s and // makes s point to the next element // in the buffer s points to. s++; return *s; }
但是,以下语句将s定义为数组并使用字符串@L_489_8@对其进行初始化.
char s[] = "abc";
数组和指针是不同的类型.数组s绑定到堆栈上分配的内存位置.它不能反弹到不同的内存位置.请注意更改变量值和更改变量名称绑定的内存位置之间的区别.在上面的函数中,您只是更改s的内容,但s本身始终指的是在堆栈上分配的固定内存位置.
s++; // in main@H_270_5@main函数中的上述语句求值为数组s的基址,即& s [0],其副作用是改变s的内容.更改s的内容意味着将变量s绑定到不同的内存位置,这始终是一个错误.任何变量在其生命周期内始终引用相同的内存位置.然而,它的内容可以改变,但那是不同的.
int main(void) { // s is an array. Arrays and pointers are // different types. initialize s with the // characters in the literal "abc" char s[] = "abc"; // equivalent to // char s[] = {'a','b','c','\0'}; // illegal operation because s is an array. // s is bound to a fixed memory LOCATIOn and // cAnnot be changed. s++; }
以上是大佬教程为你收集整理的const char数组名是函数中的const值吗?全部内容,希望文章能够帮你解决const char数组名是函数中的const值吗?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。