簡體   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