c - 结构有什么问题?

标签 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'

最佳答案

我已经修改了你的代码并指出了所有错误。 PL。看看这是否有帮助。我还添加了一个功能 打印 DVD。

#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");


    }

}

关于c - 结构有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22437613/

相关文章:

c - 我的 C 代码中的段错误

c - 如何使 `make` 为文件夹中的每个 C 文件创建一个可执行文件?

我可以拦截 C 中的全局指针初始化吗?

将 ull 转换为 mpz_t

计算具有多个空格的字符串中的单词数

c - libev ev_io_stop() 不关闭套接字

c - 从函数返回二维数组

C: scanf 添加到总数

c - 递归树函数?

指针与整数比较错误