使用结构实现集合,但它给出了错误

时间:2014-07-17 05:32:44

标签: 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;
}

7 个答案:

答案 0 :(得分:1)

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

并且您不应该在此处访问struct member数组作为结构点。

答案 1 :(得分:0)

您尝试访问

  st->array[st->index]->data1

st->array[st->index]属于int类型,您无法访问->data2


在您的主体中,您实例化内部数组大小为5的strut set,然后将其传递给pushIntToSet(),代码应为:

void pushintoset(struct set *st)                      
{   
  int item1; 
  int item2;
  int i;

  if (set == NULL) //checking for null ptr
    return ;
  printf("Enter two numbers for set [%d] \n", i);
  scanf("%d %d", &item1, &item2);
  st->data1 = item1;
  st->data2 = item2;
}

它可能不是你想要做的,但数组只是一个int数组,与data1 / data2无关。

答案 2 :(得分:0)

你能更具体一点吗?

你想在这些方面做什么

st->array[st->index]->data1 = item1;
st-array[st->index]->data2 = item2;
st->array[st->index]->index = i;

st是指向struct set的指针,你可以访问它的变量

st-> structure member

您可以通过这种方式访问​​data1和data2

st->data1 and st->data2

答案 3 :(得分:0)

data1data2是设置结构的一部分。他们不是st->array的成员。 st->array的类型为int *。您应该尝试st->data1st->data2

答案 4 :(得分:0)

您正在创建单个指针数组,而不是直接存储值,而是存储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] = item1;
st->array[st->index] = item2;
st->array[st->index] = i;
}

}

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

}


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

在这里看到它

https://ideone.com/TXBpsP

答案 5 :(得分:0)

当你看三行时:

st->array[st->index]->data1 = item1;
st-array[st->index]->data2 = item2;
st->array[st->index]->index = i;

你应该发现错位。

st - array

引用未定义的变量array。这就是为什么你提到array未定义的原因。您错过了编译所需的>

当你修复它时,你会遇到st->array是整数指针的问题;您试图将其视为指向结构类型的指针数组。

答案 6 :(得分:0)

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] is ok
        // but st->array[st->index]->data1 is wrong
        // array is a pointer of int as your define, 
        // it isn't a strcut, have no member data1 and data2
        // and the other probelm, wild pointer!
        // you use it as a array without initialize it (Memory allocation)
        st->array[st->index]->data1 = item1; // ??
        st-array[st->index]->data2 = item2;
        st->array[st->index]->index = i;
    }

}