ANSI C警告:从不兼容的指针类型赋值

w6lpcovy  于 2023-03-29  发布在  其他
关注(0)|答案(2)|浏览(158)

我正在使用ANSI C与gcc编译器,并得到警告:从不兼容的指针类型为我的代码赋值。
注意:结构体、类型定义和指针都是给我的,所以我不能修改它们。

#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;
}
k97glaaz

k97glaaz1#

stock_list_ptr * slist_ptr = ...所以slist_ptr是指向股票列表的指针
tm->stock只是一个股票列表指针。
你也应该古勒为什么铸造malloc的结果是一个坏主意。
还有:

/* 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;
8cdiaqws

8cdiaqws2#

问题数量
1.缺少定义

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

2.缺少返回类型。

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

3.OP声明了一个指针,当OP需要一个指针时,它指向一个指针。

//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.怀疑其他问题。

相关问题