传递 strcpy 的参数 2 使指针来自整数而不进行强制转换
passing argument 2 of strcpy makes pointer from integer without a cast
这是我正在做的全部代码,我正在尝试创建一个歌曲库,将用户输入的内容放入文件中。现在编译器说传递参数 2 的 strcpy 使指针来自整数而不进行强制转换,我不知道为什么。你也可以检查我的链接列表中的结构。我在链表上很菜鸟:(
#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;
}
您的函数定义与您传递的参数不匹配。应该是
void add_song(int SongID, char *Title, char *Artist, char *Composer, \
char *Album, char *Genre, int Rating, char *Remarks) {
...
...
}
另一个问题是 songID
在 main()
中未初始化。从未初始化的变量读取是 undefind behaviour.
您可能面临的另一个问题是,如果 space 可用,fgets()
会将换行符 \n
读入缓冲区,这可能会出现问题。
需要注意的事项,如果需要,您需要 trim。
没有遍历整个代码,但是strcpy问题很明显。根据函数 strcpy (http://www.tutorialspoint.com/c_standard_library/c_function_strcpy.htm) 的定义,它需要两个字符指针(也就是 C 中的字符串)
char *strcpy(char *dest, const char *src)
您只将 char 参数传递给函数 add_song,这不是指针。更改函数的签名 add_song 然后你应该可以使用 strcpy
除了一件事 "Remark",这是您代码的工作副本。在执行 scanf for Remark 之前进程退出。像我在这里所做的那样更改您的参数,并将 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;
}
这是我正在做的全部代码,我正在尝试创建一个歌曲库,将用户输入的内容放入文件中。现在编译器说传递参数 2 的 strcpy 使指针来自整数而不进行强制转换,我不知道为什么。你也可以检查我的链接列表中的结构。我在链表上很菜鸟:(
#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;
}
您的函数定义与您传递的参数不匹配。应该是
void add_song(int SongID, char *Title, char *Artist, char *Composer, \
char *Album, char *Genre, int Rating, char *Remarks) {
...
...
}
另一个问题是 songID
在 main()
中未初始化。从未初始化的变量读取是 undefind behaviour.
您可能面临的另一个问题是,如果 space 可用,fgets()
会将换行符 \n
读入缓冲区,这可能会出现问题。
需要注意的事项,如果需要,您需要 trim。
没有遍历整个代码,但是strcpy问题很明显。根据函数 strcpy (http://www.tutorialspoint.com/c_standard_library/c_function_strcpy.htm) 的定义,它需要两个字符指针(也就是 C 中的字符串)
char *strcpy(char *dest, const char *src)
您只将 char 参数传递给函数 add_song,这不是指针。更改函数的签名 add_song 然后你应该可以使用 strcpy
除了一件事 "Remark",这是您代码的工作副本。在执行 scanf for Remark 之前进程退出。像我在这里所做的那样更改您的参数,并将 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;
}