【问题标题】:Printf linkedList in C ErrorsC 错误中的 Printf linkedList
【发布时间】:2015-08-05 12:39:59
【问题描述】:

我正在尝试从文件中读取数据。文件的每一行包括: string1 string2 float 例如:A1 A2 5.22 我正在尝试将链表的第一个元素的值打印到屏幕上,但每次出现错误时:

在“program.c”文件中—— 错误:在非结构或联合中请求成员“权重”

printf("%f", data -> weight);

或 在“main.c”文件中 - 错误:取消引用指向不兼容类型的指针

printf("%f\n", data ->weight);

也许有人可以帮助我将会员数据输出到屏幕上。问题可能出在哪里,我该如何纠正?因为我尝试阅读有关此主题的其他答案,尝试了不同的变体,但对“数据”成员没有任何结果。

已编辑:我通过更改解决的问题: typedef 结构节点* 节点;
到 typedef 结构节点节点;

但是“main.c”的错误: 错误:取消引用指向不兼容类型的指针 仍然存在。也许有人知道我该如何更正我的代码?

修改后的代码:

main.c

#include <stdio.h>
#include <stdlib.h>
#include "program.h"

int main(int argc, char *argv[] ){
    if(argc != 3){return 0;}
    node* data;
    data = getData(argv ,&data);
    printf("%f \n", data -> weight); //here second mentioned error appears

return 0;
}

程序.h

#ifndef program_h
#define program_h

#include <stdio.h>
#include <stdlib.h>

#include "program.h"

typedef struct node node;  

node* getData (char* argv[], node** data);

#endif

程序.c

#include "program.h"

struct node                                          
{   
    char* from;
    char* to;
    float weight;
    struct node *next;
};

node* getData (char* argv[], node** data){

    node* elem; 
    node* lastElem;

    FILE *in=fopen(argv[1], "r");
    if (in == NULL) {
        fprintf(stderr, "Can't open input file !\n");
        exit(1);
    } 

    char* string1 = (char*)malloc(100*sizeof(char));
    char* string2 = (char*)malloc(100*sizeof(char));;
    float dataW; // dataWeigth

    fscanf(in, "%s" ,string1);
    fscanf(in, "%s" ,string2);
    lastElem = malloc( sizeof(struct node));
    lastElem -> next = NULL;
    lastElem -> from = string1;
    *data = lastElem;
    printf("%f",(*data)->weight); 


    if(!feof(in)){
        fscanf(in, "%f%*[^\n]" ,&dataW);
        lastElem -> to = string2;
        lastElem -> weight = dataW;
        while (!feof(in)) 
         {
            fscanf(in, "%s" ,string1);
            fscanf(in, "%s" ,string2);
            fscanf(in, "%f%*[^\n]" ,&dataW);
            elem = malloc( sizeof(struct node));
            elem -> next = NULL;
            elem -> from = string1;
            elem -> to = string2;
            elem -> weight = dataW;
            lastElem -> next = elem;
            lastElem = elem;
         }
    }
    fclose(in);
 return *data; 

}

【问题讨论】:

  • 1. main.c 没有看到 struct 2 的定义。node* 是 struct node**,这就是为什么你不能这样取消引用它。
  • @user3109672 我不明白为什么,因为我认为我正在将“数据”地址传递给函数,所以 main.c 应该看到定义...
  • typedef struct node* node; - 这很糟糕。要么只输入名称:typedef struct node node;,要么在指针前面加上 p,这是一个常见的约定:typedef struct node* pNode;
  • @szczurcio 谢谢,它确实有帮助,但 main.c 的错误仍然存​​在。我已经编辑了我的代码。也许你对这个错误有一些想法?
  • 您也可以使用fscanf() & sscanf() 从文件中获取输入,这不是很容易吗?

标签: c linked-list printf


【解决方案1】:

改变

struct node                                          
{   
    char* from;
    char* to;
    float weight;
    struct node *next;
};

typedef struct                                          
{   
    char* from;
    char* to;
    float weight;
    struct node *next;
} node;

将其移至 program.h,并且不要将 program.h 本身包含在内 - 这没有任何意义。相反,请将其包含在 main.cprogram.c 中。

【讨论】:

    【解决方案2】:

    嗯.. 我无法在 main.c 或 program.h 的任何地方看到 program.c 链接

    因为它应该在那里执行结构。 . . 和.. 没有您将“数据”定义为结构的地方。 因为它应该是一个

    struct node* data;
    

    或者

    node* data;
    

    在函数中调用任何东西都不能使那个东西成为结构..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-20
      • 2017-10-14
      • 2013-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      相关资源
      最近更新 更多