warning: passing argument 1 of 'strcpy' lässt Zeiger von Ganzzahl ohne cast

Bin ich mit ANSI-C und immer "warning: passing argument 1 of 'strcpy' lässt Zeiger von Ganzzahl ohne cast" für meine folgenden code:

#define MAX_LINE_SIZE 1024
#define DELIMITER ","
#define TICKET_NAME_LEN 40
#define TICKET_ZONE_LEN 10

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;

char temp_line[MAX_LINE_SIZE];
char *token;
int i, count = 0;

stock_node * snode = NULL;
struct stock_data * sdata = NULL;

FILE *stock_file = fopen( stockfile, "r" );

while (fgets(temp_line, MAX_LINE_SIZE, stock_file) != NULL) {

  token = strtok (temp_line, DELIMITER);
  count++;

  snode = (stock_node *) malloc(count * sizeof(stock_node));
  if (snode == NULL) { abort(); }  

  snode->data = (struct stock_data *) malloc(sizeof(struct stock_data));
  if (snode->data == NULL) { abort(); }  

  i = 1;

  while(token != NULL) {
     switch(i) {
        case 1:
           strcpy(snode[count - 1].data->ticket_name, token);
           break;
        case 2:
           strcpy(snode[count - 1].data->ticket_type, token);
           break;
        case 3:
           strcpy(snode[count - 1].data->ticket_zone, token);
           break;
        case 4:
           strcpy(snode[count - 1].data->ticket_price, token);
           break;
        case 5:
           strcpy(snode[count - 1].data->stock_level, token);
           break;                                                            
     }
     token = strtok (NULL, DELIMITER);
     i++;
  }
}

Bekomme ich die Warnung für die Linien:

strcpy(snode[count - 1].Daten->ticket_type, token); (als ticket_type char)
strcpy(snode[count - 1].Daten->ticket_price, token); (als ticket_price ist unsigned int)
strcpy(snode[count - 1].Daten->stock_level, token); (als stock_level ist unsigned int)

Weiß ich, warum die (Art der) allerdings weiß ich nicht, wie es zu beheben 🙁


Die Lösung war die änderung der Schalter, so dass es liest:

 switch(i) {
    case 1:
       strcpy(snode[count - 1].data->ticket_name, token);
       break;
    case 2:
       snode[count - 1].data->ticket_type = token[0];
       break;
    case 3:
       strcpy(snode[count - 1].data->ticket_zone, token);
       break;
    case 4:
       snode[count - 1].data->ticket_price = atoi(token);
       break;
    case 5:
       snode[count - 1].data->stock_level = atoi(token);
       break;                                                            
 }
Meinst du ticket_type char oder char *? Wenn char das ist, was die Ursache für dein problem. ein char ist auch wirklich ein int
Sie können nicht kopieren Sie eine Zeichenfolge in eine Ganzzahl mit strcpy. Sie müssen wandeln es über so etwas wie strtoul.
Bitte nicht umgewandelt den Rückgabewert von malloc() in C.

InformationsquelleAutor Oscar | 2013-11-04

Schreibe einen Kommentar