char *和char []之间的区别 [英] Difference between char* and char[]

查看:104
本文介绍了char *和char []之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这是一个非常基本的问题。我对以下内容为何以及如何有所不同感到困惑。

I know this is a very basic question. I am confused as to why and how are the following different.

char str[] = "Test";
char *str = "Test";


推荐答案

char str[] = "Test";

个字符的数组,初始化为

char *str = "Test";

是指向文字(常量)字符串 Test的指针。

is a pointer to the literal (const) string "Test".

它们之间的主要区别是,第一个是数组,另一个是指针。数组拥有其内容,该内容恰好是 Test 的副本,而指针仅引用字符串的内容(在这种情况下是不可变的)。

The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test", while the pointer simply refers to the contents of the string (which in this case is immutable).

这篇关于char *和char []之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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