簡體   English   中英

C strcpy() 指向一個指針? 這是如何運作的?

[英]C strcpy() to a Pointer? How does this work?

這是我正在閱讀的書中的一個示例,用於演示堆 memory 的使用。

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

int main(int argc, char *argv[])
{
    char *char_ptr;
    int *int_ptr;
    int mem_size;

    if (argc < 2) //if not given an argument, defaukt memory size is 50//
       mem_size = 50;
    else
        mem_size = atoi(argv[1]);

    printf("\t[+] allocating %d bytes of memory on the heap for char_ptr\n", mem_size); //memory is given, and passed to char pointer//
    char_ptr = (char *)malloc(mem_size);

    if(char_ptr == NULL) { //check for error//
        fprintf(stderr, "Error: could not allocate memory.\n");
        exit(-1);
   }

    strcpy(char_ptr, "This memory is located on the heap.");
    printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr);

    printf("\t[+] allocating 12 bytes of memory on the heap for int_ptr\n");
    int_ptr = (int *)malloc(12);

    if(int_ptr == NULL) {
        fprintf(stderr, "Error: coud not allocate heap memory.\n");
        exit(-1);
    }

    *int_ptr = 31337;
    printf("int_ptr (%p) --> %d\n", int_ptr, *int_ptr);

    printf("\t[-] freeing char_ptr's heap memory...\n");
    free(char_ptr);

    printf("\t[+] allocating another 15 bytes for char_ptr\n");
    char_ptr = (char *)malloc(15);

    if(char_ptr == NULL) {
        fprintf(stderr, "Error: coud not allocate heap memory.\n");
        exit(-1);
    }

    strcpy(char_ptr, "new memory");
    printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr);

    printf("\t[-] freeing int_ptr's heap memor...\n");
    free(int_ptr);
}

我對這一行感到困惑: strcpy(char_ptr, "This memory is located on the heap.");

char_ptr已經包含了已分配堆 memory 的地址,那么這個文本是如何復制到指針中的呢? printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr); . 這是否基本上意味着指針包含兩個值? 這是如何運作的?

kingvon@KingVon:~/Desktop/asm$ ./a.out
        [+] allocating 50 bytes of memory on the heap for char_ptr char_ptr (0x55ef60d796b0) --> 'This memory is located on the heap.'
        [+] allocating 12 bytes of memory on the heap for int_ptr int_ptr (0x55ef60d796f0) --> 31337
        [-] freeing char_ptr's heap memory...
        [+] allocating another 15 bytes for char_ptr char_ptr (0x55ef60d79710) --> 'new memory'
        [-] freeing int_ptr's heap memor...
        [-] freeing char_ptr's heap memory...

這是否基本上意味着指針包含兩個值?

char_ptr變量只能有一個特定的值——這將是(在成功調用malloc )memory 中指定字節數的塊的起始地址。 就是這樣——一個地址。

可能讓您感到困惑的是該地址在您的后續代碼中是如何使用/解釋的。

strcpy(char_ptr, "This memory is located on the heap."); 行在將第二個參數(字符串文字)中的數據復制到從該地址開始的字節中時使用該地址 - 並且它會繼續這樣做,直到在該文字末尾找到nul終止符(它也將復制,然后停止)。

printf("char_ptr (%p) --> '%s'\n", char_ptr, char_ptr); call 有點微妙。 Although you are giving the same address (exactly) as both arguments, the way that address is interpreted by the printf function is determined by the corresponding format arguments. 對於第一個實例, %p說明符告訴 function 打印地址的實際 對於第二個實例, %s說明符告訴它按順序打印出字符,從給定地址開始,並在找到第一個nul (零)字符時停止。

strcpy將 null 終止的 C 字符串復制到第一個參數指向的 memory 中。

不,它不包含兩個值。 指針有自己的值,即 memory 地址,它指向 memory 地址處的值。 這可以是一件事,也可以是一系列事情。 在字符指針的情況下,它是數組中的第一個字符。 一個帶有 NUL 的字符數組,也就是末尾的 0 字符是一個 C 字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM