0

I am writing a program in C, and I have some errors which I don't understand. Here is my main:

#include "res.h"
#include <stdio.h>
#include <conio.h>

int main(){
Kitchen List;
char *name = "Productname";
float price = 5.6, qwan = 4;
List.head = NULL;

CreateProduct(name, price, qwan, &List);



getch();
return 0;
}

Here is my header file:

#include <stdio.h>
#ifndef _res
#define _res


typedef struct Portion{             
    char *pName;
    float pPrice;
    float pQuantity;
    Portion *next;
}Portion;

typedef struct Kitchen{            
    Portion *head;
    int size;
}Kitchen;

void get_lost(char *string);
/*
Name : get_lost
Output: Void
Algorithem: 
*/
void CreateProduct(char *name, float price, float quantity, Kitchen *List);
/*
Name : CreateProduct
Output: Void
Algorithem:
*/

#endif

Here is my res.c:

#include "res.h"

//Prints an error msg.
void get_lost(char *string){
    printf("%s", string);
    getch();
    exit(1);
}

//Creates nodes of portion type.
void CreateProduct(char *name, float price, float quantity, Kitchen *List){
    Portion *temp = List->head;

    Portion *newPortion = (Portion*)malloc(sizeof(Portion));               //Allocating memory for new node.
    if (newPortion == NULL)
        get_lost("Memory allocation has failed!");

    newPortion->pName = (char*)malloc(sizeof(char)*strlen(name) + 1);     //Allocating memory for portion name.
    if (newPortion == NULL)
        get_lost("Memory allocation has failed!");
    strcpy(newPortion->pName, name);                                      //Copy name of product to pName.

    newPortion->pPrice = price;                                          //Initializing Portion price.
    newPortion->pQuantity = quantity;                                    //Initialzing Portion quantity.


    while (List->head != NULL)
    {                                                                   //We check if portion is already in the list.
        if (strcmp(List->head->pName, newPortion->pName))
        {
            printf("This portion already exists.");
            return;
        }
        List->head = List->head->next;
    }

    List->head = temp;                                                   //We saved the head of the list in a tempoaray variable.
    newPortion->next = List->head;                                      //Add newPortion to the list.
    List->head = newPortion;
    List->size++;                                                       //We count the number of portions in the list.
}

I get the following errors and I don't understand why:

Error   26  error C2143: syntax error : missing ')' before '*'
Error   27  error C2081: 'Kitchen' : name in formal parameter list illegal  
Error   28  error C2143: syntax error : missing '{' before '*'  

Can someone help me find out what is wrong?

Jongware
  • 22,200
  • 8
  • 54
  • 100
Yakirbu
  • 182
  • 1
  • 3
  • 14

2 Answers2

3

Change:

typedef struct Portion{             
    char *pName;
    float pPrice;
    float pQuantity;
    Portion *next;
}Portion;

to:

typedef struct Portion{             
    char *pName;
    float pPrice;
    float pQuantity;
    struct Portion *next; // <<<
}Portion;

Also note that you shouldn't cast the result of malloc in C, so change e.g.

Portion *newPortion = (Portion*)malloc(sizeof(Portion));

to:

Portion *newPortion = malloc(sizeof(Portion));

or preferably:

Portion *newPortion = malloc(sizeof *newPortion);
Community
  • 1
  • 1
Paul R
  • 208,748
  • 37
  • 389
  • 560
0

You have made a common mistake. typedef struct Portion{ }; is a self-referential structure as it has a pointer *next of the type struct Portion So, when you declare the structure as

typedef struct Portion{             
    char *pName;
    float pPrice;
    float pQuantity;
    Portion *next;
}Portion;

compiler doesn't yet know what type Portion is.

So you have to declare it as,

typedef struct Portion{
    char *pName;
    float pPrice;
    float pQuantity;
    struct Portion *next;
}Portion;

I would prefer to typedef it as Portion_t instead of Portion. It is though a very conflicted issue that whether you should use typedef for structs and unions, since as an independent developer I need to know if it is a struct or a union.

WedaPashi
  • 3,561
  • 26
  • 42