#include<stdio.h>
struct a
{
void *ptr;
unsigned long val;
};
void main()
{
unsigned char errno;
struct a *id;
id = malloc(sizeof(*id));
func2(id);
printf("After changing %d\n", id->val);
}
void func2(struct a *id)
{
unsigned char errno;
func(id,&errno);
}
void func(struct a *id,void *ptr)
{
memset(id, 0, sizeof(*id));
id->ptr = ptr;
if (sizeof(id->val) >= sizeof(id->ptr))
{
id->val = (unsigned long)id->ptr;
return;
}
}
当我在主要功能中打印 id->val 时,它正在打印 -1075050593 。但我试图访问一个无效的地址。请解释。我对c编程很陌生。