结构有什么问题?

时间:2014-03-16 13:42:35

标签: c

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


struct dvdtype{
    int dvdcode; 
    char title[50]; 
    int customerID; 
    int daysowned; 
};

struct dvdstruct{
    struct dvdtype *dvd; 
    int numdv;
};

void initDvds(dvdstruct dvds);

int main() {
    dvdstruct dvds;
    initDvds(dvdstruct dvds);
    system("PAUSE");    
    return 0;
}

void initDvds(dvdstruct dvds){
 int i;

 dvdstruct dvd[];
 int dvd[];

 dvd[]= (int *)malloc(5);
 for(i=0; i<5; i++)
 dvds.dvd[i].dvdcode=-1;
 dvds.dvd[i].title= '0';
 dvds.dvd[i].customerID=-1;
 dvds.dvd[i].daysowned=-1;
 dvds.numdvds=0;       
}

我有这个错误:

在函数'int main()'中:

21  21  [Error] expected primary-expression before 'dvds'

在函数'void initDvds(dvdstruct)'中:

29  16  [Error] storage size of 'dvd' isn't known 

32  6   [Error] expected primary-expression before ']' token

35  19  [Error] incompatible types in assignment of 'char' to 'char [50]'

38  7   [Error] 'struct dvdstruct' has no member named 'numdvds'

4 个答案:

答案 0 :(得分:0)

如果您想在C中使用已定义的结构,则必须在其前面加上关键字struct(例如,在main中,您需要在更多地方执行此操作):

int main() {
    struct dvdstruct dvds;    // add "struct" here
    initDvds(dvds);           // no need to provide type in function call
    system("PAUSE");    
    return 0;
}

作为简写,您可以typedef将其定义为新类型:

typedef struct dvdstruct {
  // definition
} dvdstruct;

然后你可以像你一样使用它。

关于存储大小:在C中声明数组时,您必须提供一个长度或在其声明中具有固有的长度。 int i[4]int i[] = { 1,2,3,4 };

关于不兼容的类型:C是一种打字语言。 charchar[50]的类型不同。 char是一个字节,char[50]实际上是指向char的指针。

答案 1 :(得分:0)

事实上它并没有一个名为“numdvds&#39;”的字段。但是,它确实有一个名为&#39; numdv&#39;。

的字段

答案 2 :(得分:0)

在C语言中,在声明变量struct

时使用struct dvdstruct dvds;关键字

您也可以使用typedef whed声明结构本身

typedef struct dvdstruct{
    struct dvdtype *dvd; 
    int numdv;
} dvdstruct_t; //define datatype

typedef struct {
    struct dvdtype *dvd; 
    int numdv;
} dvdstruct; 

答案 3 :(得分:0)

我修改了你的代码并指出了所有的错误。 PL。看看这是否有帮助。我还添加了一个函数 打印dvds。

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


struct dvdtype{
    int dvdcode;
    char title[50];
    int customerID;
    int daysowned;
};

struct dvdstruct{
    struct dvdtype *dvd;
    int numdv;
};

/*Changed the function prototype, since the argument needs to be updated
inside the function and C supports only Call By value*/
void initDvds(struct dvdstruct *dvds);

/*Added this function*/
void printDvds(struct dvdstruct *dvds);

int main() {
    struct dvdstruct dvds;
    /*You need to populate the above structure inside the function,
    so you need to pass the address of the structure as the argument */
    initDvds(&dvds);
    system("PAUSE");
    printDvds(&dvds);
    return 0;
}


void initDvds(struct dvdstruct *dvds)
{
    int i;

    /*You have made error here
    dvd[]= (int *)malloc(5);*/

    /*I am assuming you want to simulate an array of
    type struct dvdtype inside the struct dvdstruct*/
    dvds->dvd = malloc(5 * sizeof(struct dvdtype));
    dvds->numdv = 0;
    for(i=0; i<5; i++)
    {

        (dvds->dvd[i]).dvdcode = -1;
        /*This is wrong since title is a character array
        dvds->(dvd[i].title)= '0';*/
        memset((dvds->dvd[i]).title, '\0', 50);
        (dvds->dvd[i]).customerID = -1;
        (dvds->dvd[i]).daysowned = -1;
        /*Will this be inside the loop like this? this I feel 
        will be used to check how many dvds you have have  dvds.numdvds=0;*/
       dvds->numdv++;
   }

}

void printDvds(struct dvdstruct *dvds)
{

    int no_of_dvds;
    int i;
    no_of_dvds = dvds->numdv;
    for (i = 0; i < no_of_dvds; i++)
    {

        printf("Dvd Code = %d ",(dvds->dvd[i]).dvdcode);
        printf("Dvd Title  = %s ",(dvds->dvd[i]).title);
        printf("Customer Id   = %d ",(dvds->dvd[i]).customerID );
        printf("Days Owned  = %d\n",(dvds->dvd[i]).daysowned );
        printf("#####################################\n");


    }

}