..."字符*海峡=&QUOT之间的差异;和char海峡[N] =" ..."? [英] Difference between char *str = "…" and char str[N] = "…"?

查看:157
本文介绍了..."字符*海峡=&QUOT之间的差异;和char海峡[N] =" ..."?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1704407/what-is-the-difference-between-char-s-and-char-s-in-c\">What是个char []和char * S用C?结果之间的差异
  指针和字符串href=\"http://stackoverflow.com/questions/4380090/question-about-pointers-and-strings-in-c\">问题


我读到C中的字符串和我很困惑。两种方式我可以申报的字符串:

 的char *海峡=这是字符串;
焦炭STR2 [20] =这是字符串;

两者有什么声明之间的区别?当字符STR2 [20] 超过的char * str中preferred


解决方案

 的char *海峡=这是字符串;

使字符串中的常量数据部分(也称为.rdata)数据不能被修改的program.This的。

 字符STR2 [20] =这是字符串;

在此类型声明的数据是preferably 存储在堆栈区程序的,如果申报的的功能范围<内部/ code>和数据部分如果全局范围内。这对数据进行修改声明。

所以,如果你有一个必要的修改数据,然后使用第二种方法。

Possible Duplicate:
What is the difference between char s[] and char *s in C?
Question about pointers and strings in C

I'm reading about the strings in C and I'm confused. I can "declare" strings in two ways:

char *str = "This is string";
char str2[20] = "This is string"; 

What is the difference between the two declarations? When would char str2[20] be preferred over char *str?

解决方案

char *str = "This is string";

Puts the string in the constant data section (also known as .rdata) of the program.This data can't be modified.

char str2[20] = "This is string";

In this type of declaration data is preferably stored in the stack area of the program, if declared inside the function scope and in data section if declared in global scope.This data can be modified.

So if you have a necessity to modify data then use the second approach.

这篇关于...&QUOT;字符*海峡=&QUOT之间的差异;和char海峡[N] =&QUOT; ...&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆