当我删除“int e = 0;”时,我在 c 中有以下程序 我得到段错误,有人知道为什么吗?它甚至没有被使用?
第二件事是这里获得前 3 个 int 的最佳方法是什么?我正在使用 memcpy,但第一个不能正常工作。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int test()
{
unsigned char string1[] = {0xce,0x01,0x00,0x00,0xe9,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x74,0x65,0x73,0x74};
//0xce,0x01,0x00,0x00 this should be the first int
//0xe9,0x00,0x00,0x00 Second int
//0x01,0x00,0x00,0x00 third int, the rest is the text "test"
int e = 0;// when I comment this one I get Segmentation fault
unsigned int int1 = 0;
unsigned int int2 = 0;
unsigned int int3 = 0;
int length = 0;
int size = 0;
size = sizeof(length);
memcpy(&int1, string1, length); printf("%d\n",int1); //should be 461
length += size;
memcpy(&int2, &string1[length], length); printf("%d\n",int2); //shoud be 233
length += size;
memcpy(&int3, &string1[length], length); printf("%d\n",int3); //should be 1
length += size;
printf("%s\n",&string1[length]);//text should be test
}
int main()
{
test();
}
当“int e = 0;”时输出低于 存在
0
233
1
test
当“int e = 0;”时输出低于 被评论
0
233
1
Segmentation fault (core dumped)