传递strcpy的参数2使整数的指针没有强制转换

时间:2015-12-01 10:34:14

标签: c linked-list strcpy

这是我正在做的整个代码,我试图创建一个将用户输入文件的歌曲库。现在编译器说,传递strcpy的参数2使得指针来自整数而没有强制转换,我不知道为什么。你也可以查看我的链表结构。即时通讯链表:(

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

3 个答案:

答案 0 :(得分:1)

您的函数定义与您传递的参数不匹配。它应该是

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

       ...
       ...
    }

另一个问题是songID中的main()未初始化。从未初始化的变量读取是undefind behaviour

您可能遇到的另一个问题是fgets()如果有可用空间可能会出现问题,则会将换行符\n读入缓冲区。 需要注意的事情,如果需要的话,你需要修剪它。

答案 1 :(得分:0)

没有完成整个代码,但strcpy问题很明显。根据函数strcpy(http://www.tutorialspoint.com/c_standard_library/c_function_strcpy.htm)的定义,它需要两个char指针(也就是C中的字符串)

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

您只将char参数传递给函数add_song,它不是指针。更改函数add_song的签名然后你可以使用strcpy

答案 2 :(得分:0)

除了“Remark”之外,这是代码的工作副本。在执行用于备注进程退出的scanf之前。像我在这里更改您的参数,并将fopen模式更改为+ w,因此如果文件不存在,则可以自动创建。 strcpy原型是`char * strcpy(char * dest,const char * src)。 所以我相应地改变了

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