gpt4 book ai didi

c - C 中字符串指针的增加值

转载 作者:行者123 更新时间:2023-11-30 14:48:02 25 4
gpt4 key购买 nike

我正在做一项作业。我有正确的工作代码,但是,我无法理解一行的作用。

函数的描述是:

该函数将 malloc 5 个字节的内存,使用 strcpy 在这 5 个字节(4 个字母加 '\0')中存储“bird”,并将字符串指针指向的值增加字符串的长度(计算得出)使用 strlen) 它指向的(因此它最终指向句子末尾的 '\0')。然后它将返回分配的内存。

这是执行上述操作的代码

char *get_word( char **string_ptr ){

char *word;

word = malloc( 5 );

strcpy( word, "bird" );

*string_ptr += strlen( *string_ptr );

return word;

}

我不明白“*string_ptr += strlen( *string_ptr );”这行是什么意思确实如此,即使有上面​​的描述。有人能给我更详细的解释那里发生的事情吗?

最佳答案

传递给get_word的参数是 char ** (大概是由于调用者传递了字符串的地址而不是指针的集合)。

由于 C 中的所有参数都是按值传递的,因此为了更改作为参数传递的指针的地址,必须传递指针的地址。否则,该函数将仅接收指针的副本(具有其自己的且非常不同的地址),并且在函数内对指针所做的任何更改在调用函数中都将不可见。

虽然您没有提供 A Minimal, Complete, and Verifiable Example (MCVE) ,我们可以从您的函数中推断出字符串的地址正在传递给 get_word ,类似:

#define MAXC 1024
...
char buffer[MAXC] = "word_one";
char *p = buffer;
char *new_word = NULL;
...
new_word = get_word (&p);

然后,在该指针的末尾附加文本(这是 *string_ptr += strlen( *string_ptr );get_word 的目的),如下所示:

strcpy (p, new_word);

结果是,缓冲区现在将包含:

"word_onebird"

实际上,通过将指针地址传递给 get_word ,指针前进到 string_ptr 中当前单词的末尾通过*string_ptr += strlen( *string_ptr );并更改为 string_ptr指向的内容会反射(reflect)在调用函数中。

如果你只通过了get_word(string_ptr)get_word 中所做的更改在调用函数中将不可见,并且那里的指针将保持不变。但是,通过传递地址string_ptrget_word(&string_ptr)get_word 的变化(通过*string_ptr = ...对原始指针本身进行操作反射(reflect)在调用函数中的原始指针中。

<小时/>

通过示例理解指针

指针只是一个普通变量,它保存其他内容的地址作为其值。换句话说,指针指向可以找到其他内容的地址。您通常会想到保存立即值的变量,例如 int a = 5; ,指针将简单地保存 5 所在的地址。存储在内存中,例如int *b = &a; 。无论指针指向什么类型的对象,它的工作方式都是相同的。 (指针,只是指针......)

如果需要更改函数内指针指向的地址而不返回新指针,则必须将原指针的地址作为参数传递,以便函数可以进行操作原始指针本身,而不是如果参数采用指针本身,则函数中将存在指针的副本。

举个例子可能会有所帮助。花一些时间来完成代码并了解原始指针如何在 get_word 中使用。 ,但 get_word_single 中仅提供原始副本。如下:

#include <stdio.h>
#include <string.h>

#define MAXC 256

/* parameter passes only the pointer p in main() */
void get_word_single (char *string_ptr)
{
printf ("in get_word_single()\n"
" address of string_ptr: %p <-- a new pointer\n"
" value in string_ptr: %p <-- changes only seen locally\n\n",
(void*)&string_ptr, (void*)string_ptr);

string_ptr += strlen (string_ptr);
}

/* parameter passes address of p in main() */
void get_word (char **string_ptr)
{
printf ("in get_word()\n"
"address of string_ptr: %p <-- a new pointer\n"
" value in string_ptr: %p <-- holding original address\n\n"
"address of *string_ptr: %p <-- dereference to expose address\n"
" value in *string_ptr: %p <-- modify to change original\n\n",
(void*)&string_ptr, (void*)string_ptr,
(void*)&(*string_ptr), (void*)*string_ptr);

*string_ptr += strlen (*string_ptr);
}

int main (void) {

char buf[MAXC] = "one", /* a 3-character word in buf */
*p = buf;

printf ("in main()\n"
"address of p: %p <-- address of the pointer itself\n"
" value in p: %p <-- address held by the pointer\n\n",
(void*)&p, (void*)p);

get_word_single (p);

printf ("in main()\n"
"address of p: %p <-- address of pointer itself unchanged\n"
" value in p: %p <-- no change in address held\n\n",
(void*)&p, (void*)p);

get_word (&p);

printf ("in main()\n"
"address of p: %p <-- address of pointer itself unchanged\n"
" value in p: %p <-- address held incremented by 3\n\n",
(void*)&p, (void*)p);

return 0;
}

示例使用/输出

$ ./bin/passaddrex
in main()
address of p: 0x7ffc8594f370 <-- address of the pointer itself
value in p: 0x7ffc8594f380 <-- address held by the pointer

in get_word_single()
address of string_ptr: 0x7ffc8594f378 <-- a new pointer
value in string_ptr: 0x7ffc8594f380 <-- changes only seen locally

in main()
address of p: 0x7ffc8594f370 <-- address of pointer itself unchanged
value in p: 0x7ffc8594f380 <-- no change in address held

in get_word()
address of string_ptr: 0x7ffc8594f348 <-- a new pointer
value in string_ptr: 0x7ffc8594f370 <-- holding original address

address of *string_ptr: 0x7ffc8594f370 <-- dereference to expose address
value in *string_ptr: 0x7ffc8594f380 <-- modify to change original

in main()
address of p: 0x7ffc8594f370 <-- address of pointer itself unchanged
value in p: 0x7ffc8594f383 <-- address held incremented by 3

关于c - C 中字符串指针的增加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50788388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com