gpt4 book ai didi

C 指针。将大数赋值给 char * 指针

转载 作者:太空宇宙 更新时间:2023-11-04 07:37:54 26 4
gpt4 key购买 nike

#include "stdio.h"
#include "malloc.h"

int main()

{

char*x=(char*)malloc(1024);
*(x+2)=3; -----------------------------> Problem with big numbers
printf("\n%d",*(x+2));
printf("\n%d",sizeof(long int));
printf("\n %ld \n\n",(long int)sizeof(long int));
}

当我在标有箭头 (-------->) 的行中给出小数字时,这工作正常,但不适用于大值。我想存储大数字。我该怎么办?

最佳答案

您正在分配一个 char 缓冲区。这样的数组的元素只能保证保存小值,最多 255。如果你想存储数值,请改用一些数字数组(例如 longint,取决于所需值的实际大小)。例如

long* x = (long*)malloc(1024 * sizeof(long));
x[2] = 319222; // x[2] is equivalent to *(x+2)

Here您可以检查 C 中所有标量数据类型的限制。

关于C 指针。将大数赋值给 char * 指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7419885/

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