不能用strcpy(X,y);在C PROGRAM

2009-04-12 7:13 pm
const char y1[]={"Y"} ,y2[]={"y"},blank[]={" "};
const int container=12,size=15;
int input;
char shoesname[size][container]={"n"},temp[12];


strcpy(shoesname[size][k],temp);

為何compiler 出現
invaild converison from 'char' to char*'
initialzing argument 1 of 'char* strcpy(char*,const char*)'

回答 (1)

2009-04-13 3:57 am
✔ 最佳答案
It is because shoesname[size][k] is of type char, while strcpy has the first argument as a pointer to a character, namely type char *.
If I understand correctly, you'd like to have a name container of 12 characters for each shoesize, which is represented by the character pointer shoesname[i] of type char*. So the first argument should then be shoesname[i]. Note that k is undefined above, and shoesname[size]... is out of array bounds.
A working example modified from your code is as follows:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
const char y1[]={"Y"} ,y2[]={"y"},blank[]={" "};
const int container=12,size=15;
int input;
char shoesname[size][container]={"n"},temp[12]="test";

strcpy(shoesname[0],temp);
printf("%s\n",shoesname[0]);
system("pause");
}
PM me if you have other questions.


收錄日期: 2021-04-13 16:33:31
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20090412000051KK00452

檢視 Wayback Machine 備份