Question

I am using ANSI C with gcc complier and getting warning: assignment from incompatible pointer type for my code.

Note: structs, typedefs and pointers are given to me. So I am not allowed to change them.

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

typedef struct tm * tm_type_ptr;
typedef struct stock_list * stock_list_ptr;
typedef struct coin * coin_list_ptr;

typedef struct tm {
    coin_list_ptr coins;
    stock_list_ptr stock;
} tm_type;

struct stock_data 
{
    char ticket_name[TICKET_NAME_LEN+1];
    char ticket_type;
    char ticket_zone[TICKET_ZONE_LEN+1];
    unsigned int ticket_price;
    unsigned int stock_level;
};

typedef struct stock_node 
{
    struct stock_data * data;
    struct stock_node * next_node;
} stock_node;

struct stock_list
{
    stock_node * head_stock;
    unsigned int num_stock_items;
};

enum coin_types {
    FIVE_CENTS=5,
    TEN_CENTS=10,
    TWENTY_CENTS=20,
    FIFTY_CENTS=50,
    ONE_DOLLAR=100,
    TWO_DOLLARS=200
};

struct coin {
    enum coin_types denomination;
    unsigned count;
};


int main(int argc, char **argv) {

    tm_type tm;
    tm_type *tm_ptr;
    tm_ptr = &tm;

    system_init(tm_ptr);

    return EXIST_SUCCESS;
}

system_init(tm_type * tm)
{

   struct coin clist;   
   struct coin_list_ptr * clist_ptr;

   clist_ptr = &clist;

   stock_node * snode = (stock_node *) malloc(sizeof(stock_node));

   snode->data = (struct stock_data *) malloc(sizeof(struct stock_data));

   struct stock_list * slist = (struct stock_list *) malloc(sizeof(struct stock_list));

   stock_list_ptr * slist_ptr = (stock_list_ptr *) malloc(sizeof(stock_list_ptr));

   slist->head_stock = snode;

   slist_ptr = &slist;

   tm = (tm_type *) malloc(sizeof(tm_type));

   tm->stock = slist_ptr;
   tm->coins = clist_ptr;
}
Was it helpful?

Solution

stock_list_ptr * slist_ptr = ... so slist_ptr is a pointer to a stock list pointer

tm->stock is just a stock list pointer.

You should also goole why casting the result of malloc is a bad idea..

Also:

 /* malloc memory (size = 1 pointer) and assign address to stock_list_ptr */
 stock_list_ptr * slist_ptr = (stock_list_ptr *) malloc(sizeof(stock_list_ptr));
 slist->head_stock = snode;
 /* assign address of slist to slist_ptr. So you have overwritten the assignment
    above, and leaked 1 pointer's worth of memory.
    stock_list_ptr slist_ptr = &slist; is proabably what you meant...
  */
 slist_ptr = &slist;

OTHER TIPS

Number of issues

1.Missing defines

#define TICKET_NAME_LEN (100)
#define TICKET_ZONE_LEN (100)
#define EXIST_SUCCESS (0)

2.Missing return type.

// system_init(tm_type * tm)
void system_init(tm_type * tm)

3.OP is declaring a pointer to a pointer when OP wants a pointer.

//struct coin_list_ptr * clist_ptr;
coin_list_ptr clist_ptr;
//stock_list_ptr * slist_ptr = (stock_list_ptr *) malloc(sizeof(stock_list_ptr));
stock_list_ptr slist_ptr = malloc(sizeof(stock_list_ptr));
//slist_ptr = &slist;
slist_ptr = slist;

4.Suspect additional issues.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top