关于字符串,字符数组 ,局部和全局地址分配问题
2012-08-12 22:48
417 查看
void main()
{
char str1[] = "abc";
char str2[] = "abc";
const char str3[] = "abc";
const char str4[] = "abc";
const char *str5 = "abc";
const char *str6 = "abc";
char *str7 = "abc";
char *str8 = "abc";
printf("%x\n",str1); //0x12ff7c
printf("%x\n",str2);//0x12ff7c
printf("%x\n",str3);//0x12ff7c
printf("%x\n",str4);//0x12ff7c
printf("%x\n",str5);//46f020
printf("%x\n",str6);//46f020
cout << ( str1 == str2 ) << endl; // 0
cout << ( str3 == str4 ) << endl; // 0
cout << ( str5 == str6 ) << endl; // 1
cout << ( str7 == str8 ) << endl; // 1
cout << ( str5 == str7) <<endl; //1
cout << ( str6 == str7) <<endl; //1
}
对于字符数组 是在局部的空间中分别的地址, 看出在局部地址中分配地址是 在栈中分配的,
但是字符串 是全局变量 ,对应的是字符串常量,保存在只读的内存区内 ,相同的字符串只能在一个地址中,所以他们的地址是相同的
{
char str1[] = "abc";
char str2[] = "abc";
const char str3[] = "abc";
const char str4[] = "abc";
const char *str5 = "abc";
const char *str6 = "abc";
char *str7 = "abc";
char *str8 = "abc";
printf("%x\n",str1); //0x12ff7c
printf("%x\n",str2);//0x12ff7c
printf("%x\n",str3);//0x12ff7c
printf("%x\n",str4);//0x12ff7c
printf("%x\n",str5);//46f020
printf("%x\n",str6);//46f020
cout << ( str1 == str2 ) << endl; // 0
cout << ( str3 == str4 ) << endl; // 0
cout << ( str5 == str6 ) << endl; // 1
cout << ( str7 == str8 ) << endl; // 1
cout << ( str5 == str7) <<endl; //1
cout << ( str6 == str7) <<endl; //1
}
对于字符数组 是在局部的空间中分别的地址, 看出在局部地址中分配地址是 在栈中分配的,
但是字符串 是全局变量 ,对应的是字符串常量,保存在只读的内存区内 ,相同的字符串只能在一个地址中,所以他们的地址是相同的
相关文章推荐
- 关于memcpy拷贝结构体、结构体数组到字符数组(字符串)的问题
- 关于动态存储分配函数的调用,在已经过排序的数组中查找及删除内容的操作,余数的分析,删除字符数组中的空格,对链表的逆置,在源字符串中查找子字符串的个数,函数指针以及函数的调用,循环赋值带来的问题以及插入
- 关于字符串 字符数组 字符型指针的问题
- 关于C语言字符数组、字符串和指针的相关问题
- 关于memcpy拷贝结构体、结构体数组到字符数组(字符串)的问题
- 关于字符串与字符数组的一些小问题
- 一个关于 字符与字符串 的 小问题
- 编写一个函数new,对n个字符开辟连续的存储空间,此函数应返回一个指针(地址),指向字符串开始的空间。new(n)表示分配n个字节的内存空间。
- 关于字符数组和字符串数组
- 关于将数组地址赋给指针的问题
- C/C++字符串,字符数组,字符指针及其相互静态拷贝与追加的安全问题解决方案(2)
- 关于数组名的值,地址,表示以及强转等,有些绕的问题
- Android studio关于含有转义字符的字符串无法Log,以及如何去掉转义字符问题
- 关于二维(字符)数组的输入问题
- 结构体中字符数组赋值字符串问题
- c语言字符指针数组经典,此后不必再担心字符串问题.
- JPasswordField 中得到的字符数组转化为字符串(密码乱码问题)
- 关于数组字符反转的问题
- C 字符数组与字符串的使用及加结束符'\0'的问题
- 关于C语言的字符数组问题