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

问题描述

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之间的差异;和char海峡[N] =" ..."?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:06