배열에서 구입할 항목의 이름 (이 예에서는)을 포함하는 구조체를 만들려고합니다. 매트릭스에서 구입해야하는 가격과 무게입니다. 이것은 내가하려는 일의 쉬운 예일뿐입니다. strcpy를 사용하여이 값을 선언 했으므로 값을 선언합니다.이 방법을 사용하는 것이 가장 좋습니다.C에서 구조체 내에서 행렬이 작동합니까?
#include<stdio.h>
#include<string.h>
typedef struct Grocery_list{
char item_name[2];
double item_info[2][2];
}Grocery;
int main(){
Grocery market;
strcpy(market.item_name[0], "Apple");
strcpy(market.item_name[1], "Sugar");
strcpy(market.item_info[0][0],200); //apple weight
strcpy(market.item_info[1][0], 3); //apple price
strcpy(market.item_info[0][1], 300);
strcpy(market.item_info[1][1], 4);
printf("%f \n",martket.item_info[1][1]);
return 0;}
오류는 내가 구글에 대한 답변과 솔루션을 검색,하지만 노력 모든 것이 다른 오류 또는 심지어 같은 원인, 분명히
teste.C: In function ‘int main()’:
teste.C:14:27: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
strcpy(market.item_name[0], "Apple");
^
In file included from teste.C:2:0:
/usr/include/string.h:129:14: error: initializing argument 1 of ‘char* strcpy(char*, const char*)’ [-fpermissive]
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
teste.C:15:27: error: invalid conversion from ‘char’ to ‘char*’ [-fpermissive]
strcpy(market.item_name[1], "Sugar");
^
In file included from teste.C:2:0:
/usr/include/string.h:129:14: error: initializing argument 1 of ‘char* strcpy(char*, const char*)’ [-fpermissive]
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
teste.C:16:35: error: cannot convert ‘double’ to ‘char*’ for argument ‘1’ to ‘char* strcpy(char*, const char*)’
strcpy(market.item_info[0][0],200);
^
teste.C:17:34: error: cannot convert ‘double’ to ‘char*’ for argument ‘1’ to ‘char* strcpy(char*, const char*)’
strcpy(market.item_info[1][0], 3);
^
teste.C:18:36: error: cannot convert ‘double’ to ‘char*’ for argument ‘1’ to ‘char* strcpy(char*, const char*)’
strcpy(market.item_info[0][1], 300);
^
teste.C:19:34: error: cannot convert ‘double’ to ‘char*’ for argument ‘1’ to ‘char* strcpy(char*, const char*)’
strcpy(market.item_info[1][1], 4);
^
teste.C:21:16: error: ‘martket’ was not declared in this scope
printf("%f \n",martket.item_info[1][1]);
^
있습니다. 프로그래밍에 익숙하지 않기 때문에 이러한 오류의 의미를 알 수 없습니다.
이 멤버item_name
는
이 개 문자의 배열이
이것은 구조체와 아무런 관련이 없습니다. 당신은 문자열과'strcpy'를 완전히 잘못 사용하고 있습니다. – user2357112
'strcpy'는 복사 ** 문자열 **입니다. –
'market.item_name [0]'은 (는) 문자입니다. 'strcpy'는'char *'또는'char []'(아마도 부정확 한) 타입의 버퍼를 복사 할 버퍼가 필요합니다. – nbro