1

I am using ANSI C and getting "warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast" for my below 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++;
  }
}

I get the warning for lines:

strcpy(snode[count - 1].data->ticket_type, token); (as ticket_type is char)
strcpy(snode[count - 1].data->ticket_price, token); (as ticket_price is unsigned int)
strcpy(snode[count - 1].data->stock_level, token); (as stock_level is unsigned int)

I know why (sort of) however I don't know how to fix it :(


The solution was to change the switch so it reads:

 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;                                                            
 }
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Oscar
  • 233
  • 2
  • 6
  • 15
  • Do you mean ticket_type is char or char *? If char that is what is causing your problem. a char is really an int – Rob Nov 04 '13 at 11:45
  • You can't copy a string into an integer with strcpy. You need to convert it via something like strtoul. – Simon Elliott Nov 04 '13 at 11:45
  • [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Nov 04 '13 at 12:03

1 Answers1

1

You should use strcpy() to copy source string into destination string.

if you want to copy string into integer value first you need to convert string into integer and then do direct assignment.

For example You can use atoi() or strtol() Functions.

Gangadhar
  • 10,248
  • 3
  • 31
  • 50
  • yes. atoi solved to problem for unsigned int however ticket_type is char and couldn't convert it yet – Oscar Nov 04 '13 at 11:51
  • if token consists single character like this `"a"=='a','\0'` you can use token[0]. if token is more than one character then you need to decide your self which character you want, if you want whole string you need to make changes in your declaration. – Gangadhar Nov 04 '13 at 11:54