c - 使用结构实现一组,但出现错误

标签 c struct

它在 pushintoset 中给出错误数组未声明的函数首次在此函数中使用 '->' 的类型参数无效(具有 'int')

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

struct set
{
    int data1;
    int data2;
    int size;
    int *array;
    int index;
};

struct set *createset(int capacity)
{
    struct set * s = (struct set*)malloc(sizeof(struct set));
     s->size = capacity;
     s->array = (int *)malloc(sizeof(int) * s->size);
    return s;
}

void pushintoset(struct set *st)       //giving error in this function that array undeclared                      
 {   
    int item1; 
    int item2;
    int i;
    for(i=0;i<5;i++)
    {
        printf("enter set %d \n",i);
        scanf("%d %d",&item1,&item2);
        st->index = i;
        st->array[st->index]->data1 = item1;
        st-array[st->index]->data2 = item2;
        st->array[st->index]->index = i;
    }
}

void printset(struct set * s)
{
    int i;
    for(i=1;i<=5;i++)
    {
        printf("%d  \t %d \n",s->array[i]->data1,s->array[i]->data2);
    }
}

int main()
{
    struct set * se = createset(5);
    if(se==NULL)``
    printf("memory not allocate");
    pushintoset(se);
    printset(se);
    return 0;
}

最佳答案

st-array[st->index]->data2 = item2; // loss of '>' after 'st-'

并且您不应该在此处将结构成员数组作为结构点进行访问。

关于c - 使用结构实现一组,但出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24795567/

相关文章:

c - 使用 OpenSSL 的 Web 服务器

c - 扩展大型 C 编程项目 - 对各种函数的 undefined reference

c - C 中的 fscanf 没有读取整行?

c++ - 简单的 "identifier ' xxx' 未定义"使用结构

c - 如何创建结构数组,其中结构的最后一个元素是 struct hack

c++ - 在原始类型模板特化之间转换

c - Printf 在 C 信号处理程序中不工作

C 网络服务器 header 发送

c++ - 包含函数作为成员的结构数组 C++

c - 如何将结构成员存储为大端