果然又报错了 2023-01-14 23:09 采纳率: 50%
浏览 29
已结题

创建散列表后无法写入,显示表达式必须具有类类型

创建散列表时后,初始化写入时提示:E0153 表达式必须具有类类型,但它具有类型 "cells"
E0153 表达式必须具有类类型,但它具有类型 "cells"

使用“.”时(表达式必须具有类类型,但它具有类型 "cells")

#include<stdio.h>
#include<stdlib.h>
typedef enum { empty, deleted, written }information;
typedef struct node* cells;
struct node {
    int data;
    information info;
};
int main() {
    cells *cell;
    cell = (cells*)malloc(sizeof(cells));
    int n, i, jude, key, data;
    scanf_s("%d", &n);
    for (i = 0; i < 11; i++) {
        cell[i].info = empty;
    }
}

使用“->”,运行时超时跳出循环,(提示正在从cell读取无效数据)

#include<stdio.h>
#include<stdlib.h>
typedef enum { empty, deleted, written }information;
typedef struct node* cells;
struct node {
    int data;
    information info;
};
int main() {
    cells *cell;
    cell = (cells*)malloc(sizeof(cells));
    int n, i, jude, key, data;
    scanf_s("%d", &n);
    for (i = 0; i < 11; i++) {
        cell[i]->info = empty;
    }
}

完整代码

#include<stdio.h>
#include<stdlib.h>
typedef enum { empty, deleted, written }information;
typedef struct node* cells;
struct node {
    int data;
    information info;
};
int main() {
    cells *cell;
    cell = (cells*)malloc(sizeof(cells));
    int n, i, jude, key, data;
    scanf_s("%d", &n);
    for (i = 0; i < 11; i++) {
        cell[i]->info = empty;
    }
    for (i = 0; i < n; i++) {
        scanf_s("%d", &jude);
        if (jude == 1) {
            scanf_s("%d", &key);
            data = key;
            while (1) {
                key = key % 11;
                if (cell[key]->info != written) {
                    cell[key]->data = data;
                    cell[key]->info = written;
                    break;
                }
                else {
                    key++;
                }
            }
        }if (jude == 2) {
            scanf_s("%d", &key);
            data = key;
            key = key % 11;
            while (data != cell[key]->data) {
                key++;
            }cell[key]->info = deleted;
        }if (jude == 3) {
            scanf_s("%d", &key);
            int count = 1;
            data = key;
            key = key % 11;
            while (data != cell[key]->data) {
                key++;
                count++;
            }printf("%d", count);
        }
    }return 0;
}

E0153 表达式必须具有类类型,但它具有类型 "cells"
尝试将“.”换成“->”,结果提示正在从cell读取无效数据,运行超时跳出循环,未成功写入。
希望有人能帮我解决这个问题(散列表的创建与查找)
  • 写回答

2条回答 默认 最新

  • 懂事的人 2023-01-14 23:29
    关注
    #include<stdio.h>
    #include<stdlib.h>
    typedef enum { empty, deleted, written }information;
    typedef struct node* cells;
    struct node {
        int data;
        information info;
    };
    int main() {
        cells *cell;
        cell = (cells*)malloc(sizeof(cells));
        int n, i, jude, key, data;
        scanf_s("%d", &n);
        for (i = 0; i < 11; i++) {
            cell[i].info = empty;
        }
    }
     
    
    

    这个代码的错误在于第13行,程序试图使用malloc分配一个指向指针的指针,但是应该分配一个指向struct node类型的指针数组。

    修改方法是:

    修改第13行,使用malloc分配一个指向struct node类型的指针数组,而不是一个指向指针的指针。
    cell = (cells*)malloc(sizeof(struct node)*11);

    修改第14行,将cell[i].info 替换为 cell[i]->info, 因为cell是一个指针数组,应该使用“->”来访问。
    for (i = 0; i < 11; i++) {
    cell[i]->info = empty;
    }

    修改第3行,将 typedef struct node* cells; 替换为 typedef struct node cells[11];

    修改第10行,将 scanf_s("%d", &n); 替换为 scanf("%d", &n);

    这样就可以解决问题了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月23日
  • 已采纳回答 1月15日
  • 请采纳用户回复 1月15日
  • 创建了问题 1月14日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。