C语言 从结构链表中打印字符串时大小为 1 的读取无效



我创建了一个结构的单链列表,我正在尝试从中打印一个字符串。我的链表定义为:

typedef struct LinkedListNode
{
void* data;
struct LinkedListNode* next;
} LinkedListNode;
typedef struct LinkedList
{
int count;
struct LinkedListNode* head;
} LinkedList;

我的结构定义为:

typedef struct
{
char missile[10]; /* trying to print this */
funcPtr missileFunc;
} missileStruct;

我的printf声明是这样的:

printf("Print this: %sn",(*(missileStruct*)((*currentNode).data)).missile);

瓦尔格林德输出:

==11680== Invalid read of size 1
==11680==    at 0x4E88CC0: vfprintf (vfprintf.c:1632)
==11680==    by 0x4E8F898: printf (printf.c:33)
==11680==    by 0x401F9E: game (game.c:53)
==11680==    by 0x401112: menu (menu.c:57)
==11680==    by 0x400A6F: main (main.c:13)
==11680==  Address 0xa656c676e6973 is not stack'd, malloc'd or (recently) free'd
==11680== 
==11680== 
==11680== Process terminating with default action of signal 11 (SIGSEGV)
==11680==  General Protection Fault
==11680==    at 0x4E88CC0: vfprintf (vfprintf.c:1632)
==11680==    by 0x4E8F898: printf (printf.c:33)
==11680==    by 0x401F9E: game (game.c:53)
==11680==    by 0x401112: menu (menu.c:57)
==11680==    by 0x400A6F: main (main.c:13)
Print this: ==11680== 

解决了!事实证明,我的一个 h 文件中的 typedef 结构中有一个错字,我在其中写了 char* 导弹而不是 char 导弹[10]。

最新更新