Victor RM Victor RM - 3 months ago 11
C Question

Do matrices work within structs in C?

I'm trying to make a struct that contains (in this example) the name of the item to buy in an array and in a matrix it's price and the weight of it I should buy. This is just an easy example of what i'm trying to do.
I use strcpy to declare the value just because I learned it this way, I don't if it's the best of the methods.

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


The errors are

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]);
^


Obviously, I searched for answers and solutions on Google, but everything I tried just caused other errors or even the same. For I'm new in programming in general I don't know what those errors mean.

Thank you in advance

Answer

The member item_name is an array of two characters, it's not an array of strings, it's just a single string (which can only hold a single character, remember that all strings have a special terminator character). You probably should make this an array of arrays of char, which means it's an array of strings:

char item_name[2][64];  // Two strings, each can hold 63 characters

The member item_info is an array of arrays of double. The strcpy function is for copying strings (arrays of char).

Remember that each element in an array is like a variable, which means you can just assign to it:

market.item_info[0][0] = 200; //apple weight
market.item_info[1][0] = 3; //apple price
market.item_info[0][1] = 300;
market.item_info[1][1] = 4;

And the last error is because you misspelled market.

All of this would be very obvious even to a beginner if you just read the error messages.

Comments