shiiranyan
shiiranyan

Reputation: 15

passing argument 2 of strcpy makes pointer from integer without a cast

this is the whole code of what im doing, im trying to create a song library that will put what the user enter into file. now the compiler says that passing argument 2 of strcpy makes pointer from integer without a cast and i dont know why. also can u check my linked list for the struct. im so noob at linked list :(

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

struct node {
    //definition of struct node to create struct node song
    int SongID;
    char Title[100];
    char Artist[100];
    char Composer[100];
    char Album[100];
    char Genre[100];
    int Rating;
    char Remarks[1000];
    struct node*next;
};

add_song(int SongID, char Title, char Artist, char Composer, char Album, char Genre, int Rating, char Remarks) {
    //this is the add song function as stated in the mp2 specs
    FILE*fp;
    fp=fopen("song.txt","r+");

    int i=1, j, choice;
    struct node* temp=malloc(sizeof(struct node));

    temp->SongID=SongID;
    fprintf(fp,"%d",SongID);

    strcpy(temp->Title, Title);
    fprintf(fp,"%s",Title);

    strcpy(temp->Artist, Artist);
    fprintf(fp,"%s",Artist);

    strcpy(temp->Composer, Composer);
    fprintf(fp,"%s",Composer);

    strcpy(temp->Album, Album);
    fprintf(fp,"%s",Album);

    strcpy(temp->Genre, Genre);
    fprintf(fp,"%s",Genre);

    temp->Rating=Rating;
    fprintf(fp,"%d",Rating);

    strcpy(temp->Remarks, Remarks);
    fprintf(fp,"%s",Remarks);

    fclose(fp);
}

int main ()
{
    struct node song;
    int choice;
    int k, i;
    int SongID;
    char Title[100];
    char Artist[100];
    char Composer[100];
    char Album[100];
    char Genre[100];
    int Rating;
    char Remarks[1000];

   /* do
     {
        printf("Enter 1 to add song, 2 to update song, or 3 to list songs: ");
        scanf("%d\n", &choice1);

        if (choice1==1)
        {*/
        srand(time(NULL));
        song.SongID=rand();

        printf("Enter Title: ");
        fgets(Title,100,stdin);

        printf("Enter Artist: ");
        fgets(Artist,100,stdin);

        printf("Enter Composer: ");
        fgets(Composer,100,stdin);

        printf("Enter Album: ");
        fgets(Album,100,stdin);

        //for easier code, numbers are being chosen as input
        printf("Press 1 for Art Music, 2 for Popular Music, or 3 for Traditional Music): ");
        scanf("%d", &choice);

        if (choice==1)
        {
            strcpy(song.Genre,"Art Music");
        }
        else if (choice==2)
        {
            strcpy(song.Genre,"Popular Music");
        }
        else if (choice==3)
        {
            strcpy(song.Genre,"Traditional Music");
        }
        else
        {
            printf("You entered a blank genre.\n");
        }

        printf("Enter your rating, choose from 1-5: ");
        scanf("%d", &Rating);

        printf("Enter Remarks: ");
        fgets(Remarks,1000,stdin);

        add_song(SongID, Title, Artist, Composer, Album, Genre, Rating, Remarks);
            /*k=0;
            break;
        }
        else if (choice1==2)
        {
            //update_song(song);
            k=0;
            break;
        }
        else if (choice1==3)
        {
           // list_songs(song);
            k=0;
            break;
        }
        else
        {
            k=1;
            printf("That is not a valid input.\n");
        }
    }while (k==1);*/

    return 0;
}

Upvotes: 0

Views: 2189

Answers (3)

Punit Vara
Punit Vara

Reputation: 4224

This is working copy of your code except one thing "Remark". Before executing scanf for Remark process exits . Change your arguments as I did here and also change fopen mode as +w so if file is not exist it can be created automatically. strcpy prototype is `char *strcpy(char *dest, const char *src). So I have changed accordingly

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

struct node {
    //definition of struct node to create struct node song
    int SongID;
    char Title[100];
    char Artist[100];
    char Composer[100];
    char Album[100];
    char Genre[100];
    int Rating;
    char Remarks[1000];
    struct node *next;
};

void add_song(int SongID, char *Title, char *Artist, char *Composer, char *Album, char *Genre, int Rating, char *Remarks) {

    //this is the add song function as stated in the mp2 specs
    FILE *fp;
    fp=fopen("song.txt","a+");

    int i=1, j, choice;
    struct node* temp=malloc(sizeof(struct node));

    temp->SongID=SongID;
    fprintf(fp,"%d",SongID);

    strcpy(temp->Title, Title);
    fprintf(fp,"%s",Title);

    strcpy(temp->Artist, Artist);
    fprintf(fp,"%s",Artist);

    strcpy(temp->Composer, Composer);
    fprintf(fp,"%s",Composer);

    strcpy(temp->Album, Album);
    fprintf(fp,"%s",Album);

    strcpy(temp->Genre, Genre);
    fprintf(fp,"%s",Genre);

    temp->Rating=Rating;
    fprintf(fp,"%d",Rating);

    strcpy(temp->Remarks, Remarks);
    fprintf(fp,"%s",Remarks);

    fclose(fp);
}

int main ()
{
    struct node song;
    int choice;
    int k, i;
    int SongID;
    char Title[100];
    char Artist[100];
    char Composer[100];
    char Album[100];
    char Genre[100];
    int Rating;
    char Remarks[1000];

   /* do
     {
        printf("Enter 1 to add song, 2 to update song, or 3 to list songs: ");
        scanf("%d\n", &choice1);

        if (choice1==1)
        {*/
        srand(time(NULL));
        song.SongID=rand();

        printf("Enter Title: ");
        fgets(Title,100,stdin);

        printf("Enter Artist: ");
        fgets(Artist,100,stdin);

        printf("Enter Composer: ");
        fgets(Composer,100,stdin);

        printf("Enter Album: ");
        fgets(Album,100,stdin);

        //for easier code, numbers are being chosen as input
        printf("Press 1 for Art Music, 2 for Popular Music, or 3 for Traditional Music): ");
        scanf("%d", &choice);

        if (choice==1)
        {

            strcpy(song.Genre,"Art Music");
        }
        else if (choice==2)
        {

             strcpy(song.Genre,"Popular Music");
        }
        else if (choice==3)
        {

            strcpy(song.Genre,"Traditional Music");
        }
        else
        {
            printf("You entered a blank genre.\n");
        }

        printf("Enter your rating, choose from 1-5: ");
        scanf("%d",&Rating);

        printf("Enter Remarks: \n");
        fgets(Remarks,1000,stdin);

        add_song(SongID, Title, Artist, Composer, Album, Genre, Rating, Remarks);
            /*k=0;
            break;
        }
        else if (choice1==2)
        {
            //update_song(song);
            k=0;
            break;
        }
        else if (choice1==3)
        {
           // list_songs(song);
            k=0;
            break;
        }
        else
        {
            k=1;
            printf("That is not a valid input.\n");
        }
    }while (k==1);*/

    return 0;
}

Upvotes: 0

Otaj
Otaj

Reputation: 46

Didn't go through the whole code, but strcpy problem is obvious. According to definition of function strcpy (http://www.tutorialspoint.com/c_standard_library/c_function_strcpy.htm), it expects two char pointers (aka strings in C)

char *strcpy(char *dest, const char *src)

You're passing only char arguments to a function add_song, which is just not the pointer. Change the signature of the function add_song and then you should be fine with strcpy

Upvotes: 0

P.P
P.P

Reputation: 121427

Your function definition don't match with the arguments you pass. It should be

    void add_song(int SongID, char *Title, char *Artist, char *Composer, \
char *Album, char *Genre, int Rating, char *Remarks) {

       ...
       ...
    }

Another issue is that songID is uninitialized in main(). Reading from an uninitialized variable is undefind behaviour.

Another problem you might face is that fgets() reads the newline \n into buffer if there's space available which might be problematic. Something to be aware of and you need trim it if nececcary.

Upvotes: 1

Related Questions