C 用指针存储的数据

C 用指针存储的数据,c,pointers,byte,bit,C,Pointers,Byte,Bit,我认为它有效。255只有一个-165535 2次-1和16777215 3次-1。在您的程序中,b的地址似乎是2686636,当您写入(char*)内存+i或(char*)&b+i时,这意味着该指针指向char,因此当您向其中添加一个时,它将只跳转到一个内存地址i.e2686637,依此类推,直到2686735(即(char*)2686636+99) 现在,当您取消引用时,即.*((unsigned int*)((char*)memory+i)),您将获得该内存地址处的值,但您只给了b值(其地

我认为它有效。255只有一个-165535 2次-1和16777215 3次-1。

在您的程序中,b的地址似乎是2686636,当您写入(char*)内存+i或(char*)&b+i时,这意味着该指针指向char,因此当您向其中添加一个时,它将只跳转到一个内存地址i.e2686637,依此类推,直到2686735(即(char*)2686636+99)


现在,当您取消引用时,即.*((unsigned int*)((char*)memory+i)),您将获得该内存地址处的值,但您只给了b值(其地址为2686636)。所有其他内存地址都有您正在打印的垃圾值


因此,首先必须在其余地址(2686637到2686735)存储一些数据 祝你好运
我希望这会有所帮助

我在昨天的评论中没有提到这一点,但很明显,从0到100的for循环超出了无符号整数的大小

我只是忽略了代码中一些明显的问题,并试图就您提出的实际问题给出提示(比在一个方便的:-上做得更困难)。不幸的是,我昨天没有时间完成这项工作。所以,我给你的提示推迟一天

尽量避免对某个类型的大小进行假设(如2字节或4字节)。即使您的假设现在成立,如果您切换编译器或切换到另一个平台,它也可能会改变。因此,在整个代码中使用sizeof(type)。有关此问题的详细讨论,您可能希望了解:。标准只要求特定类型应能容纳的范围(0-65535表示无符号int),因此仅为类型指定最小大小。这意味着int的大小可能(特别是)大于2字节。Beyond primitive types sizeof还可以帮助您计算结构的大小,其中由于内存对齐和打包,结构的大小可能与您通过查看其属性而“预期”的大小不同。所以sizeof操作符是你的朋友

确保在printf中使用正确的格式

小心使用指针算术和强制转换,因为结果取决于指针的类型(显然取决于与之相加的整数的值)

(无符号整数*)内存+1!=(无符号字符*)内存+1

(unsigned int*)内存+1==(unsigned char*)内存+1*sizeof(unsigned int)

下面是我将如何编写代码:

0028FF94,  , -1
0028FF95,  , -1
0028FF96,  , -1
0028FF97,  , 0
0028FF98,  , 0
0028FF99,  , 0
0028FF9A,  , 0
0028FF9B,  , 0
//为了便于说明,请检查int在我们的平台上有多大
printf(“Sizeof int:%d字节\n”,Sizeof(unsigned int));
//我们用无符号int的最大可表示值初始化b
//包括UINT_最大值
无符号整数b=UINT_MAX//0xFFFFFF(如果sizeof(unsigned int)为4)
//我们打印出值及其十六进制表示形式
printf(“B=%u 0x%X\n”,B,B);
//我们获取b的地址并将其存储在一个空指针中
void*内存=&b;
int i=0;
//我们循环从地址b开始的无符号字符,直到sizeof(b)
//(在我们的例子中,b是unsigned int)使用sizeof(b)更好,因为如果我们更改b的类型
//我们不必记住更改for循环中的sizeof。循环的工作原理是一样的
for(i=0;i打印值255)发生。
printf(“%p,%d\n”,(无符号字符*)内存+i,*((无符号字符*)内存+i));
}

我希望你会觉得这很有帮助。祝你的作业顺利,我希望你学到了一些现在和将来都可以使用的东西:-)。

只删除未签名的int?现在的输出是2686636,-1和2686637,-1i执行此打印(“%p,%c,%d\n”,(char*)内存+i,*((char*)内存+i),*((char*)内存+i));它给出了2686636,,-1,但我注意到当我做b=65535时,前2个地址是空白,-1但当我做b=4294967295时,前4个地址是空白,-1和4294967295是32位的,所以我认为它可以工作。我更改了代码,我认为它现在可以工作了。chceck这个问题我在末尾贴了出来。我很惊讶第二部分可以工作!为什么?我试着用这个(char)记忆'a';而不是一些数字和输出是0028FF94,a,97和所有其他存储单元是0。因此,我认为这是好的,它的工作就像它应该。
void *memory;
void *abc;

abc=memory;
for(i=0;i<100;i++){
*(int*)abc=0;
abc++;
}

*(int*)memory=16777215;

for(i=0;i<100;i++){
    printf("%p, %c, %d\n", (char*)memory+i, *((char *)memory +i), *((char *)memory +i));
}
0028FF94,  , -1
0028FF95,  , -1
0028FF96,  , -1
0028FF97,  , 0
0028FF98,  , 0
0028FF99,  , 0
0028FF9A,  , 0
0028FF9B,  , 0
//check how big is int on our platform for illustrative purposes
printf("Sizeof int: %d bytes\n", sizeof(unsigned int));

//we initialize b with maximum representable value for unsigned int
//include <limits.h> for UINT_MAX
unsigned int b =  UINT_MAX; //0xffffffff (if sizeof(unsigned int) is 4)

//we print out the value and its hexadecimal representation
printf("B=%u 0x%X\n", b, b);

//we take the address of b and store it in a void pointer
void* memory= &b; 

int i = 0;

//we loop the unsigned chars starting at the address of b up to the sizeof(b) 
//(in our case b is unsigned int) using sizeof(b) is better since if we change the type of b
//we do not have to remember to change the sizeof in the for loop. The loop works just the same
for(i=0; i<sizeof(b); ++i)
{
    //here we kept %d for formating the individual bytes to represent their value as numbers
    //we cast to unsigned char since char might be signed (so from -128 to 127) on a particular 
    //platform and we want to illustrate that the expected (all bytes 1 -> printed value 255) occurs.
    printf("%p, %d\n", (unsigned char *)memory + i, *((unsigned char *) memory + i));
}