【发布时间】:2016-05-24 18:19:57
【问题描述】:
有谁知道为什么成员Node_ptr next; 使数组poly[1] 和poly[2] 的元素显示错误的值?如果我从结构 (struct node) 中删除 Node_ptr next;,我可以得到索引 1 和 2 的正确值。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct node *Node_ptr;
struct node {
int coef;
int exp;
Node_ptr next;
};
int main()
{
struct node p1_terms[] = {10, 1000, 5, 14, 1, 0};
struct node p2_terms[] = {3, 1990, 2, 1492, 11, 5};
struct node poly[20];
poly[0] = p1_terms[0];
poly[1] = p1_terms[1];
poly[2] = p1_terms[2];
printf("Your polynomials are: \n%dx^%d+%dx^%d+%dx^%d", poly[0].coef, poly[0].exp, poly[1].coef, poly[1].exp, poly[2].coef, poly[2].exp);
int siz = sizeof(poly);
printf("\n\nSize of the array: %d bytes \n",siz);
return 0;
}
【问题讨论】:
-
{10, 1000, 5, 14, 1, 0};初始化列表错误。 -->{{10, 1000}, {5, 14}, {1, 0}}
标签: c arrays initialization structure