Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/58.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
理解char*、char[]和strcpy()_C_Arrays_String_Pointers_Strcpy - Fatal编程技术网

理解char*、char[]和strcpy()

理解char*、char[]和strcpy(),c,arrays,string,pointers,strcpy,C,Arrays,String,Pointers,Strcpy,我的理解如下: char*指向字符串常量,修改它指向的数据时未定义。但是,您可以更改它指向的位置 char[]指可以更改的内存块。您可以更改它的内容,但不能更改它所指的内容 strcpy(dest,src)将src复制到dest中 我的问题是,将strcpy()与dest一起使用char*是不正确的,它已经指向了某个东西(因为我相信旧内容将被strcpy()覆盖,这是未定义的行为) 例如: char *dest = malloc(5); dest = "FIVE"; char *src =

我的理解如下:

  • char*
    指向字符串常量,修改它指向的数据时未定义。但是,您可以更改它指向的位置

  • char[]
    指可以更改的内存块。您可以更改它的内容,但不能更改它所指的内容

  • strcpy(dest,src)
    src
    复制到
    dest

我的问题是,将
strcpy()
dest
一起使用
char*
是不正确的,它已经指向了某个东西(因为我相信旧内容将被
strcpy()
覆盖,这是未定义的行为)

例如:

char *dest = malloc(5);
dest = "FIVE";

char *src = malloc(5);
src = "NEW!";

strcpy(dest, src); /* Invalid because chars at dest are getting overwritten? */
char *dest = malloc(5);
dest = "FIVE";

char *src = malloc(5);
src = "NEW!";

strcpy(dest, src); /* Invalid because chars at dest are getting

不幸的是,你的理解并不完全正确

char*
指向字符数据,因为其中没有
const
,所以可以写入指向的数据

但是,完全有可能做到这一点:

char *a = "hello";
这为您提供了一个指向只读数据的读/写指针,因为字符串文本存储在只读内存中,而不是语言语法“认为”的常量

最好将上述内容写为:

const char *a = "hello";
为了更清楚地说明您不能修改
a
指向的数据

另外,您的示例混合了
malloc()
和赋值是错误的

这:

是糟糕的代码,您永远不应该这样做。它只是用一个指向字符串
“FIVE”
的指针覆盖由
dest
返回的指针,该字符串作为字符串文本存在于内存中的某个位置(也是只读的)

使用字符串数据初始化新分配的内存的正确方法是使用
strcpy()

请注意,检查
malloc()
的返回值是一个好主意

对同一个内存进行多次写操作没有问题,这是C语言的一个非常基本的概念;变量代表内存,可以通过“重写”在不同时间赋予不同的值

简单到:

int a = 2;

printf("a=%d\n", a);
a = 4;
printf("a=%d\n", a);
演示了这一点,当然它也适用于字符串,因为它们只是内存块

您可以扩展上面基于
malloc()
的示例:

char *dest = malloc(5);
if(dest != NULL)
{
  strcpy(dest, "five");
  printf("dest='%s'\n", dest);
  strcpy(dest, "four");
  printf("dest='%s'\n", dest);
  strcpy(dest, "one");
  printf("dest='%s'\n", dest);
}
它将打印:

dest='five'
dest='four'
dest='one'

char*是指向内存地址的指针,因此您可以修改该地址中包含的信息

char*和char[]之间的区别在于char[]不是动态的,不能更改其大小。此外,char*指向堆中的地址,而char[]存储在程序堆栈中

您可以将strcpy与指针和数组一起使用,它可以工作,因为来自这两个指针和数组的数据都可以被覆盖

我的理解如下:

  • char*
    指向字符串常量,修改它指向的数据时未定义。但是,您可以更改它指向的位置
这里你指的是一个像

char * string = "mystring";
你做的是正确的
string[1]='r'未定义。但这并不是因为
char*
,而是因为字符串文字以某种方式被放入只读内存

将此与

char string[] = "mystring";
其中,我在RAM中定义了一个数组,将所述字符串放入其中。这里允许执行
string[1]='r',因为我们在正常的数据内存中

这似乎支持了你的假设,但假设:

char string[] = "mystring";
char * string2 = string;
这里
string2[1]='r'
是有效的,因为它指向一个写入也可以的位置

是的,因为这里的名称只是变量的名称,而不是指针

我的问题是,在dest为 char*已经指向了某个东西(因为我相信旧的 内容将被未定义的strcpy()覆盖 行为

这取决于你所说的“已经指向某物”是什么意思

例如:

char *dest = malloc(5);
dest = "FIVE";

char *src = malloc(5);
src = "NEW!";

strcpy(dest, src); /* Invalid because chars at dest are getting overwritten? */
char *dest = malloc(5);
dest = "FIVE";

char *src = malloc(5);
src = "NEW!";

strcpy(dest, src); /* Invalid because chars at dest are getting
覆盖*/

这里你又把几件事搞混了

首先,您将
dest
指向一个全新的内存块。之后,您将它指向无法写入的其他地方,并且内存块丢失(内存泄漏)

同样的情况也发生在
src

因此,
strcpy()
失败

你能行

char *dest = malloc(5);

char *src = "NEW!";

strcpy(dest, src);

这里,
dest
指向一个可写的位置,
src
指向有用的数据。

快速分析:

char *dest = malloc(5);
// 'dest' is set to point to a piece of allocated memory
// (typically located in the heap)
dest = "FIVE";
// 'dest' is set to point to a constant string
// (typically located in the code-section or in the data-section)
int i = 5;
i = 6;
您将变量
dest
赋值两次,因此很明显,第一次赋值没有意义

就像写作一样:

char *dest = malloc(5);
// 'dest' is set to point to a piece of allocated memory
// (typically located in the heap)
dest = "FIVE";
// 'dest' is set to point to a constant string
// (typically located in the code-section or in the data-section)
int i = 5;
i = 6;

除此之外,您还“丢失”了分配内存的地址,因此以后将无法释放它。

char*指向一个字符串常量
-Nope。适当设置的
char*
指向
char
。它是否是适当终止的
char
序列是它所寻址的数据的产物。它不是常数。数组的一般描述(在您的列表中:
char[]
)更接近实际情况。指针包含一个地址;数组是地址。首先,char*是指向char的指针。在C语言中,字符串是以零字符结尾的字符序列,因此通常char*指向此类字符串的开头。但它也可以指向一个缓冲区的开始,该缓冲区用于接受这样一个字符串。如果字符串不是文字,则可以修改。@WhozCraig“arrays是地址”-呃,数组有地址arrays不是地址;它们是由非零数量的elements@user93353我知道我的理解力很差,我是个初学者。把C作为我的第一语言是很难的。
char*a=“hello”一种向后兼容性实际上可能会受到质疑的情况,“不幸的是,您的理解并不完全正确。”我是这么认为的。我在这里学习,我不希望自己是正确的:)所以使用