Informations

Auteur(s) Alexandre Gobeaux
Date limite Pas de date limite
Limite de soumission Pas de limite
Étiquettes de catégories S3, Level 2, Malloc, Pointer, Struct

Etiquettes

Se connecter

[S3] Structures and pointers to structures

Here you will be playing with structures and pointers to structures so that you can understand better how to use one instead of another. You'll have to return the attributes of both and you'll have to compare them.

typedef struct product {
    char *name;
    double price;
} product_t;

Hints :


Question 1: Get Price (pointer to the structure)

Write the body of the getPtrPrice function.

/*
 * @ptrProd != NULL
 * @return: the price of the product ptrProd
 */
double getPtrPrice(product_t *ptrProd);
Question 2: Get Price

Write the body of the getPrice function.

/*
 * @return: the price of the product prod
 */
double getPrice(product_t prod);
Question 3: Get Name (pointer to the structure)

Write the body of the getPtrName function.

/*
 * ptrProd != NULL
 * @name in ptrProd != NULL
 * @return: a string on the heap that contains the name of the product ptrProd or NULL if an error happens
 */
char* getPtrName(product_t *ptrProd);
Question 4: Get Name

Write the body of the getName function.

/*
 * @name in prod != NULL
 * @return: a string on the heap that contains the name of the product ptrProd or NULL if an error happens
 */
char* getName(product_t prod);
Question 5: Product Equals

Write the body of the prodEquals function.

/*
 * @ptrProd != NULL
 * @name in ptrProd != NULL
 * @name in prod != NULL
 * @return: 1 if the two products are the same, i.e., they have the same price and the same name
 */
int prodEquals(product_t *ptrProd, product_t prod);