繁体   English   中英

C中字符串指针的值增加

[英]Increasing Value of String Pointer in C

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

功能描述为:

此函数将分配5个字节的内存,使用strcpy将“ bird”存储在这5个字节(4个字母加'\\ 0')中,并将字符串指针指向的值增加字符串的长度(使用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中的所有参数都是按值传递的,因此,为了更改作为参数传递的指针的地址,必须传递指针的地址 否则,该函数将仅接收指针的副本(具有其自身的地址和完全不同的地址),并且对该函数内指针所做的任何更改在调用函数中都将不可见。

虽然您没有提供“最小,完整和可验证的示例(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中的get_word ),类似于:

strcpy (p, new_word);

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

"word_onebird"

因此,实际上,通过将指针的地址传递给get_word ,该指针通过*string_ptr += strlen( *string_ptr );前进到string_ptr当前单词的string_ptr *string_ptr += strlen( *string_ptr ); 并将string_ptr指向的位置更改反映回调用函数中。

如果只传递了get_word(string_ptr) ,则在get_word所做的更改将在调用函数中不可见,并且指针将保持不变。 但是,通过使地址的 string_ptrget_word(&string_ptr)在变更get_word (其通过对原始指针本身操作*string_ptr = ...被反映在原始指针返回调用函数。


通过示例了解指针

指针只是一个普通变量,将其他地址作为其值。 换句话说,一个指针指向可以找到其他内容的地址。 通常您会想到一个包含立即数的变量,例如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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM