如何申报C字符串 [英] How to declare strings in C

查看:121
本文介绍了如何申报C字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
  内存分配的char *和char []

谁能给我解释一下什么是code

Can anyone explain me what is a difference between these lines of code

char *p = "String";
char p2[] = "String";
char p3[7] = "String";

在什么情况下,我应该用上面的每?

In what case should I use each of the above ?

推荐答案

此链接应该满足你的好奇心。

This link should satisfy your curiosity.

基本上(忘记你的第三个例子是坏的),1和2之间的不同在于1的指针数组分配空间。

Basically (forgetting your third example which is bad), the different between 1 and 2 is that 1 allocates space for a pointer to the array.

但在code,你可以操纵他们为指针都是一样的 - 唯一的事情,你不能重新分配第二个

But in the code, you can manipulate them as pointers all the same -- only thing, you cannot reallocate the second.

这篇关于如何申报C字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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