簡體   English   中英

如何將值轉換為整數類型的字符串?

[英]how to convert a value to integer type string?

我們都知道要轉換字符串中的值,我們可以執行以下操作

char* buffer = ... allocate a buffer ...
int value = 4564;
sprintf(buffer, "%d", value);

但是如果我想將數據轉換為整數緩沖區而不是字符緩沖區,那我該怎么辦呢?

int* buffer = ... allocate a buffer ...
int value = 4564;
sprintf(buffer, "%d", value);

提前致謝

確保將緩沖區定義為“值”的值,而不是指向“值”地址的指針。 見下文:

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

int main(int argc, char** argv)
{
    /* Allocate memory. */
    int* buffer = malloc(sizeof(int));

    int value = 1000;

    /* Sets the number stored in buffer equal to value. */
    *buffer = value;

    /* prints 1000 */
    printf("%d\n", *buffer);

    /* Change value to show buffer is not pointing to the address of 'value'. */
    value = 500;

    /* Still prints 1000. If we had used 
    int* buffer = &value, it would print 500. */
    printf("%d\n", *buffer);

    return 0;
}

暫無
暫無

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

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