【问题标题】:trying to print a array of structs in C试图在 C 中打印一个结构数组
【发布时间】:2017-07-14 16:49:58
【问题描述】:

我被要求构建一个函数,该函数接收一个带有很多零的静态二维数组,并将其转换为一个结构数组。每个结构都包含不为零的值和列的索引。

现在我已经构建了它,但问题在于打印功能。

1) 当我尝试打印两次时,它只打印一次,第二次列表变为 NULL。为什么会出现这种情况?

    print(list);  
    print(list);

2) 为什么我不能像在 main 函数中那样打印?

printf("this is just a print |%d||%d|  ", list[0]->next->next->next->data, list[0]->col);

为什么我无法访问它,程序崩溃...

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//#include <vld.h>
#include <string.h>
#include <ctype.h>
#define C 5
#define N 4

typedef struct Node {
    int data;
    int col;
    struct Node *next;
} node;

node **fun(int arr[N][C]) { 
    int i, j, k;
    node **list;
    node *temp;

    list = (node**)calloc(N, sizeof(node *));

    for (i = 0; i < N; i++) {
        list[i] = NULL;
        for (j = C - 1; j >= 0; j--)
            if (arr[i][j] != 0) {
                temp = (node*)malloc(sizeof(node));
                temp->data = arr[i][j];
                temp->col = j;
                temp->next = list[i];
                list[i] = temp;
            }
    }
    return list;
}

void print(node **head) {
    int i;
    node **temp = head;
    for (i = 0; i < N; i++) {
        while (temp[i]) {
            printf("|%d||%d|  ", temp[i]->data, temp[i]->col);
            temp[i] = temp[i]->next;
        }
        printf("\n\n");
    }
}

void main() {
    int arr[N][C] = { {0,0,4,0,7}, {3,0,0,0,0}, {9,1,0,6,0} , {0,0,0,0,0} };
    node **list;
    list = fun(arr);

    print(list);  ///////////
    print(list);  ///////////////

    printf("this is just a print |%d||%d|  ", list[0]->next->next->next->data, list[0]->col);
}

【问题讨论】:

  • 关于第二个项目 - 你可能在某个时候访问了一个空的“next”,导致它崩溃......如果你在 linux 上,尝试用 gdb 调试它......
  • temp[i]=temp[i]-&gt;next; 破坏性地改变了列表的结构。
  • 另外,显然你的下一个指向自己,而不是下一个节点......不确定我是否能理解你的代码,但显然这是在填充节点时发生的......跨度>

标签: c arrays struct printing


【解决方案1】:

正如 cmets 中提到的,您在打印它们的过程中破坏了指针列表:

    while(temp[i])
    {   printf("|%d||%d|  ",temp[i]->data,temp[i]->col);
        temp[i]=temp[i]->next;    // <---- here
    }

每个temp[i] 都与head[i] 相同,因此您可以在执行此操作时修改原始列表。当该值为 NULL 时,while 循环退出,因此最终结果是所有数组元素都为 NULL。

您需要将此值分配给一个临时值,以便您可以在不更改列表的情况下遍历列表:

    node *temp2 = temp[i];
    while(temp2)
    {   printf("|%d||%d|  ",temp2->data,temp2->col);
        temp2=temp2->next;
    }

【讨论】:

  • 感谢您的修复工作,哦,所以我使用 Temp[i] 运行并在 Null 中运行...我明白了,非常感谢您
【解决方案2】:

您的print 函数修改了数组:它使用数组元素遍历列表,并为它们留下NULL 值。

这是一个更正的版本:

void print(node **head) {
    int i;
    for (i = 0; i < N; i++) {
        node *temp;
        for (temp = head[i]; temp; temp = temp->next) {
            printf("|%d||%d|  ", temp->data, temp->col);
        }
        printf("\n\n");
    }
}

【讨论】:

  • @EddieKnaz:סלח,我修改了与 C99 之前的编译器兼容的答案。你应该得到一个更现代的编译器,你使用什么环境?
  • 哦,在 2017 年与它运行,对不起,但我使用 2010 年它;我们只使用 2010 年的学校
  • Visual Studio 2010 在采用 C99 标准 10 年后问世,但仍未实现其大部分改进。微软游说委员会加入其 secure API,但并不关心实现大多数更有用的扩展。他们又花了 5 年的时间才终于让他们进来。这里有一个详细的解释:herbsutter.com/2012/05/03/reader-qa-what-about-vc-and-c99
猜你喜欢
  • 2011-07-18
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 2010-11-25
  • 1970-01-01
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
相关资源
最近更新 更多