中国Linux论坛首页 技术论坛| 文章荟萃| 藏经阁| 项目计划| 在线调查| 网站镜像| 软件仓库| 关于本站|

Linux 高级应用
   >> Linux 嵌入技术
此话题阅读次数: 2303

此话题的页数:1 | 2 | (显示全部)   打印
qkfw68
(enthusiast)
09-04-22 17:23
define a string  

const char temp[]="abc";

#define TEMP "abc"

const char *temp="abc";

以上三种定义常量字符串的方法有啥区别?

文章选项: 打印

zhoukejun
(stranger)
09-04-22 19:31
Re: define a string 新 [re: qkfw68] 

我来试试。
第一个:temp 是一个数组,该数组内容为const char 型,初始化后不能对temp[i]赋值, 可以将temp 指向别的地址。
第二个:编译时,编译器会用"abc" 替换TEMP。
第三个:temp 为指针,指向const char 型, 可以将temp 指向别的地址。

PS: 试试cdecl 这个软件,可以解释复杂的C语言声名。


/*-------------------------------------------------------------------------------*/
第一个:temp 是一个数组,该数组内容为const char 型,初始化后不能对temp[i]赋值, 也不可以将temp 指向别的地址。

原来我一直把
const char temp[] = "aaa";
const char *temp = "aaa";
这两种情况搞混了。

编辑者: zhoukejun (09-04-23 13:17)

文章选项: 打印

qkfw68
(enthusiast)
09-04-22 19:39
Re: define a string 新 [re: zhoukejun] 

第一个:temp 是一个数组,该数组内容为const char 型,初始化后不能对temp[i]赋值, 可以将temp 指向别的地址。
-------------
可以将temp指向别处?

文章选项: 打印

adiosET
(member)
09-04-22 21:36
Re: define a string 新 [re: qkfw68] 

应该不行的
声明成数组后
temp是个常量指针,不能更改

文章选项: 打印

1help1
(veteran)
09-04-23 09:23
Re: define a string 新 [re: qkfw68] 

(1)test case 1:
const char temp[]="abc";
int main()
{
temp[0]='c';
printf("temp %s \n",temp);
}
debian:~# gcc -o test test.c
test.c: In function `main':
test.c:8: error: assignment of read-only location `temp[0]'

(2))test case 2:
const char temp[]="abc";
char temp1[]="def";
int main()
{
temp = temp1;
printf("temp %s \n",temp);
}
debian:~# gcc -o test test.c
test.c: In function `main':
test.c:8: error: assignment of read-only variable `temp'

(3))test case 3:
const char* temp="abc";
char temp1[]="def";
int main()
{
temp = temp1;
printf("temp %s \n",temp);
}
debian:~# gcc -o test test.c
debian:~# ./test
temp def

(4))test case 4:
const char* temp="abc";
int main()
{
temp[0] = 'd';
printf("temp %s \n",temp);
}
debian:~# gcc -o test test.c
test.c: In function `main':
test.c:8: error: assignment of read-only location `*temp'


去年去一家公司面试的时候 被问到过。





--------------------
http://vm-kernel.org/blog/

文章选项: 打印

qkfw68
(enthusiast)
09-04-23 09:53
Re: define a string 新 [re: 1help1] 

help兄,
你写的几个case,可能大家都知道。
我想问的是:我们在使用上面列出的三种定义常量字符串的时候有什么讲究吗?
或者说别人问你,你问什么用#define TEMP "abc"
而不用const char *temp="abc", const char temp[]="abc" 呢?

另外,第3个case:
-----------------------------------
test case 3:
const char* temp="abc";
char temp1[]="def";
int main()
{
temp = temp1;
printf("temp %s \n",temp);
}
debian:~# gcc -o test test.c
debian:~# ./test
temp def
-------------------------------
如果我们把temp定义成全局的变量,然后我们把temp指向了数组temp1[],
这种情况下,会导致系统为"abc"分配的空间泄漏吗?

文章选项: 打印

1help1
(veteran)
09-04-23 10:02
Re: define a string 新 [re: qkfw68] 

>这种情况下,会导致系统为"abc"分配的空间泄漏吗?

泄露?没有吧。泄露一般是对 动态分配的区域来说的,这里 abc 的区域是 本来就不需要通过 free 释放的。



--------------------
http://vm-kernel.org/blog/

文章选项: 打印

adiosET
(member)
09-04-23 10:11
Re: define a string 新 [re: qkfw68] 

你问什么用#define TEMP "abc"
~~~~~~~~~~~~~
这个一般常用吗?,编译器并没有为“abc\0"分配空间啊

文章选项: 打印

qkfw68
(enthusiast)
09-04-23 10:18
Re: define a string 新 [re: 1help1] 

没有描述准确,呵呵。
应该是我们会丢失"abc"的地址,也就是无法再得到这个字符串。这个问题应该是存在的吧?

文章选项: 打印

qkfw68
(enthusiast)
09-04-23 10:20
Re: define a string 新 [re: adiosET] 

这个还是比较常用的,
这里我们不会给"abc"分配空间,只是在用到这个宏的地方,进行替换,
如果整个CODE都没有用到这个宏,那么跟没有定义是一个样的。

文章选项: 打印

此话题的页数:1 | 2 | (显示全部)   打印

前往讨论区

Contact Us LINUXFORUM.NET