1
struct item {
    char name[100];
    double price;
} item;


char name[100];
int shelf;
int slot;
float price;
int NumOfShelves = 50
int NumOfSlotsPerShelf = 2
struct item *arrayl = (int *) malloc(NumOfShelves * NumOfSlotsPerShelf * sizeof(int));

//this is my "arraylist" ^ i think

printf("Add an item name);
scanf("%s", &name);
printf("Add an item price);
scanf("%f", &price);
printf("Add the shelf number of the item");
scanf("%d", &shelf);
printf("Add the slot number");
scanf("%d", &slot);

//on this line. How do I add an item to that slot and shelf in my arrayl?

printf("Search for an item by first giving the shelf number:");
scanf("%d", &slot);
printf("Search by giving the slot number");
scanf("%d", &slot);

//if arrayl contains the location

//print name and price of item in that location

else {
printf("None")
return 0;
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • 1
    Your question is unclear. Do you want an array of `struct item`? And I don't see any 2D array here whatsoever... Please read this: [ask] and then [edit] your question and clarify. – Jabberwocky Oct 22 '19 at 13:08
  • int *arrayl = (int *) malloc(NumOfShelves * NumOfSlotsPerShelf * sizeof(int)); Isn't this an array? Yes, I want an array of struct items. How would I go about this? –  Oct 22 '19 at 13:08
  • Yes that's more or less an array of `int` but not an array of `struct item`s. See answer below. – Jabberwocky Oct 22 '19 at 13:11
  • To be clear, `int *arrayl = (int *) malloc(NumOfShelves * NumOfSlotsPerShelf * sizeof(int))`; _Isn't this an array?_ No, it is a collection of pointers to specific locations in memory. `int array1[10][20] = {0};` is an example of a `C` array. Both allow use of index notation to access _elements_ of the collection, but only one is an actual array. – ryyker Oct 22 '19 at 13:37
  • @johnjacob Please do **not** make significant changes to a question that has already been answered. It invalidates the answer given which ruin the whole QA idea. If you still have problems after using the advice in the answer, you should instead write a new question. – Support Ukraine Oct 24 '19 at 06:43
  • understood, thanks. –  Nov 01 '19 at 17:32

1 Answers1

3

Your array has the wrong type. Use

struct item *arrayl = malloc(NumOfShelves * NumOfSlotsPerShelf * sizeof *array1);
^^^^^^^^^^^                                                              ^^^^^^

Then you simply do:

array1[shelf * NumOfSlotsPerShelf + slot].price = price;

Kind of the same for the name but you need strcpy instead of =

strcpy(array1[shelf * NumOfSlotsPerShelf + slot].name, name);

An alternative way of doing malloc is:

 int NumOfShelves = 50;
 int NumOfSlotsPerShelf = 2;
 struct item (*array1)[NumOfSlotsPerShelf] = malloc(NumOfShelves * sizeof *array1);

This will allow you to write items in the array like:

array1[shelf][slot].price = price;
strcpy(array1[shelf][slot].name, name);

note

Using malloc means that all items in the array are initially uninitialized. It may be a better idea to use calloc so that all items are zero initialized.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • The index should be `shelf * NumOfSlotsPerShelf + slot`, shouldn't it? – hager Oct 22 '19 at 13:16
  • Hi. Thanks so much! I decided to use the second Malloc as it provides an easier way to search for items. The only problem I see is that i am unable to use the first methods you suggested (regarding adding items to the array) on the second malloc. How would I go about doing that? –  Oct 22 '19 at 13:26
  • @johnjacob The answer shows how to write to items in the array for both types of malloc. – Support Ukraine Oct 22 '19 at 13:30
  • @johnjacob if you are asking how you would change the memory to allow more items after it has already been `malloc`'ed, _[realloc](https://www.tutorialspoint.com/c_standard_library/c_function_realloc.htmhttps://www.tutorialspoint.com/c_standard_library/c_function_realloc.htm)_ is how you would do that. But be aware, of some of the subtleties of using it: _[How to use realloc](https://stackoverflow.com/questions/13748338/how-to-use-realloc-in-a-function-in-c)_. – ryyker Oct 22 '19 at 13:30
  • you have to change the '.' to -> I believe –  Oct 22 '19 at 13:36
  • @johnjacob No, it has to be `.` Why do you think it has to be `->` ? – Support Ukraine Oct 22 '19 at 14:03
  • @johnjacob If you get an error then you did not copy my code correctly. see https://ideone.com/DstFXh – Support Ukraine Oct 23 '19 at 05:35
  • https://ideone.com/xj3PBM on line 53 an 54. the error is: member reference base type 'struct item' [NumOfSlotsPerShelf] is not a structure or union. –  Oct 23 '19 at 16:52
  • I know that this code is different than the one i wrote on stack overflow, it is slightly more advanced –  Oct 23 '19 at 16:53