954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

integer without a cast

I'm getting a the error "passing arg1 of 'strcpy' makes pointer from integer without a cast....

#include<stdio.h>
#include <stdlib.h>

struct node {
       char make;
       char model;
       int  year;
       char color;
       char plate;
       struct node* next;
};
  
void menu();
void insert(struct node** head, char make, char , int, char, char);

int main(void){
    
    struct node* head = NULL;
    char make, model,plate,sfname,fname,color;
    int x,year;
    menu();
    scanf("%d",&x);
    
    switch(x){
              
                   case 1:
                             printf("Enter make: ");
                             scanf("%s",&make);
                             printf("Enter model: ");
                             scanf("%s",&model);
                             printf("Enter year: ");
                             scanf("%d", &year);
                             printf("Enter color: ");
                             scanf("%s", &color);
                             printf("Enter license plate number");
                             scanf("%s",&plate);
                             insert(&head,make,model,year,color,plate);
                             break;
                   
                   case 2:   printf("Enter the license plate number of the vehicle to remove");
                             scanf("%s ", &plate);
                             //del();
                             break;
                   
                   case 3:   printf("Here are all vehicles:\n");
                             //printcars();
                             break;
                   
                   case 4:   printf("Enter a filename to save as ");
                             scanf("%s ",&sfname);
                             //savetfile();
                             break;
                   
                   case 5:   printf("Enter filename to load ");
                             scanf("%s ",&fname);
                             break;
                   
                   case 0:   printf("Thank you using this program!");
                             break;
    default: "Not an options";
    
    }
    
    system("PAUSE");
    return 0;
}

void insert(struct node** head, char make,char model, int year, char color, char plate){
     
     struct node* newnode;
     struct node* crnt;
     
     newnode = (struct person*)malloc(sizeof(struct node));
     strcpy(make,newnode->make);
     strcpy(model,newnode->model);
     //newnode->year = year;
     strcpy(color, newnode->color);
     strcpy(plate, newnode->plate);

 
     
     newnode->next = crnt->next;
     crnt->next = newnode;
     
     
}


void menu(){
     
     printf("Here are your options:\n1) Insert a vehicle\n2) Delete a vehicle\n3) Print all vehicles\n4) Save to file\n5) Load from file\n0) Quit\n");
     
}
Ballar32
Newbie Poster
4 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

You forgot to include string.h.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
scanf("%s",&make);
scanf("%s",&model);
scanf("%s", &color);
scanf("%s",&plate);
scanf("%s ",&sfname);

These will have an effect that you did not intended.
The format %s is for strings or arrays of type char, and when you pass it to scanf you don't need the & operator. Futhermore you have declared those variables as a single type char. In these case it would be scanf("%c", &model) as an example. Change the argument in the scanf() and make all those single char variables into arrays of type char, is what the rest of you code seems to indicate.

scanf("%s ",&sfname);

Eliminate the extra space between thes and the closing "

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Also we normally discourage the use of scanf() with"%s" because it will allow you to corrupt the program's memory by scribbling outside the boundry of the arrays. For example if make is declared as an array of 10 characters and you type 15 characters then scanf() will write the extra 5 characters somewhere in memory -- where is anyone's guess. A second disadvantage of scanf() with "%s" is that it will not capture more than one word -- it stops copying from the keyboard at the first white-space character. So you can not enter a make that has two or more spaces in the name.

The solution is to either get the characters one character at a time from the keyboard buffer (hard way) or call fgets() which will limit the input to the size of the program's input buffer.

char make[10];
printf("Enter make\n");
fgets(make, sizeof(make), stdin);

Now, one disadvantage of fgets() is that it will insert the '\n' character at the end of the input buffer if there is enough room. So you have to strip it back out

if( make[strlen(make)-1] == '\n')
    make[strlen(make)-1] = 0;
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
I'm getting a the error "passing arg1 of 'strcpy' makes pointer from integer without a cast....



This is where you get the error right

strcpy(make,newnode->make);
strcpy(model,newnode->model);

strcpy will get two char pointer (char *) as an argument. But you are passing just a (char). Which in fact is wrong. The char is internal converted to ASCII that is an int. Hence you get that an error.

In your case, since you are just copy a single char. You could just use a assignment operator to assign value. So for example you could replace the above statment by this

newnode->make = make;
newnode->model = model


strcpy are basically used to copy from one stirng to another, but not from one char to another. :) And offcouse others advice should be considered as well. In fact they are very important on improving your code.


ssharish

ssharish2005
Posting Whiz in Training
253 posts since Dec 2006
Reputation Points: 73
Solved Threads: 20
 

I've included , changed from single characters to arrays, and i'n still getting the same error.

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

struct node {
       char make[16];
       char model[16];
       int  year;
       char color[16];
       char plate[16];
       struct node* next;
};
  
void menu();
void insert(struct node** head, char make, char , int, char, char);

int main(void){
    
    struct node* head = NULL;
    char make, model,plate,sfname,fname,color;
    int x,year;
    menu();
    scanf("%d",&x);
    
    switch(x){
              
                   case 1:
                             printf("Enter make: ");
                             scanf("%s", &make);
                             printf("Enter model: ");
                             scanf("%s", &model);
                             printf("Enter year: ");
                             scanf("%d", &year);
                             printf("Enter color: ");
                             scanf("%s", &color);
                             printf("Enter license plate number: ");
                             scanf("%s", &plate);
                             insert(&head,make,model,year,color,plate);
                             break;
                   
                   case 2:   printf("Enter the license plate number of the vehicle to remove");
                             scanf("%s ", &plate);
                             //del();
                             break;
                   
                   case 3:   printf("Here are all vehicles:\n");
                             //printcars();
                             break;
                   
                   case 4:   printf("Enter a filename to save as ");
                             scanf("%s ",&sfname);
                             //savetfile();
                             break;
                   
                   case 5:   printf("Enter filename to load ");
                             scanf("%s ",&fname);
                             break;
                   
                   case 0:   printf("Thank you using this program!");
                             break;
    default: "Not an options";
    
    }
    printf("It does get to this point");
    system("PAUSE");
    return 0;
}

void insert(struct node** head, char make,char model, int year, char color, char plate){
     
     struct node* newnode;
     struct node* crnt;
     crnt=*head;
     
     newnode = (struct person*)malloc(sizeof(struct node));
     strcpy(make,newnode->make);
     strcpy(model,newnode->model);
     newnode->year = year;
     strcpy(color, newnode->color);
     strcpy(plate, newnode->plate);

     if(crnt->head == NULL)
                   crnt->next=newnode;
                   crnt= crnt->next;
    
 
     
     newnode->next = crnt->next;
     crnt->next = newnode;
     
     
}


void menu(){
     
     printf("Here are your options:\n1) Insert a vehicle\n2) Delete a vehicle\n3) Print all vehicles\n4) Save to file\n5) Load from file\n0) Quit\n");
     
}
Ballar32
Newbie Poster
4 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

> changed from single characters to arrays, and i'n still getting the same error.
But you didn't change the char parameters to char arrays at the same time.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

What do you mean?

Ballar32
Newbie Poster
4 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

>I've included
So you have.

>changed from single characters to arrays
I can't really help you at this point. Your misunderstanding of data types is so complete and fundamental that it's impossible to write programs until you get a book on C and actually read it.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
What do you mean?

he means like this (note I added the asterisks to make, model and plate because all three require character arrays):

void insert(struct node** head, char *make,char *model, int year, char color, char *plate){
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thank you all I no longer have that problem

Ballar32
Newbie Poster
4 posts since Sep 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You