gpt4 book ai didi

c - 似乎 printf 使我的程序出现错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:35:09 27 4
gpt4 key购买 nike

<分区>

我正在学习结构和链表。但是,我遇到了一个问题,阻止我调试程序的错误,因为它似乎来自函数 printf,我用它来调试程序。

下面的程序运行良好:

struct pointer_struct
{
struct new_struct *ptr;
};

struct new_struct
{
int i;
struct new_struct *ptr;
};

void init(struct pointer_struct *pointer, int nb)
{
struct new_struct my_struct;
my_struct.i = nb;
my_struct.ptr = NULL;
pointer->ptr = &my_struct;
}

int main(void)
{
struct pointer_struct pointer;
pointer.ptr = NULL;

init(&pointer, 15);
//printf("pointer.ptr = %p\n", pointer.ptr);
printf("pointer.ptr->i = %d\n", pointer.ptr->i);
}

输出:

pointer.ptr->i = 15

但是一旦我取消对注释行的注释,i 就会采用奇怪的值。以下是一些输出示例:

$./a.out
pointer.ptr = 0x7fffc6bcc650
pointer.ptr->i = -448723664
$./a.out
pointer.ptr = 0x7fffd09ed480
pointer.ptr->i = 1218512176
$./a.out
pointer.ptr = 0x7ffff630fa70
pointer.ptr->i = -1073674960

printf 出了什么问题?

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