1

I've created a 2D array using this:

struct Cars (*ParkingLot)[FloorCount] = malloc(sizeof(struct Cars[FloorCount][10]));

I don't know if it matters but the FloorCount is set to 1 for now, and struct Cars is defined this way:

    struct Cars
{
    int ID;
    char color[20];
    char type[20];
};

Anyway, I'm trying to use this array in a function, and I can't access the values inside the array. For example, the next thing:

void CarIn (struct Cars *Lot[])
{
   printf("%d", Lot[0][0].ID);
}
ParkingLot[0][0].ID=15;
CarIn(ParkingLot);

That's not what I wanna do, but it's the most basic function I can think of using the array, it will help me with the rest.

Edit: Well, I've managed to print using the function, all I needed is to add & before the Lot[0][0].ID... The other problem I have now is that this function doesn't seem to work at all, it always crash:

void CarIn (struct Cars *Lot[],struct Cars New)
{
    Lot[0][0]=New;
    return;
}
Voxito
  • 33
  • 8

2 Answers2

1

First, your sizing is wrong in your allocation declaration for ParkingLot. I assume you want 10 cars per floor and an arbitrary number of floors, and if that is the case, then this should be done as:

struct Cars (*ParkingLot)[10] = malloc(FloorCount * sizeof *ParkingLot);

ParkingLot is a pointer to an array of 10 Cars (not 10 pointers to Cars; rather 10 Cars in sequence). sizeof *ParkingLot will calculate the memory requirement for holding that many sequential Cars structures, and finally, multiplying by FloorCount will give you that many floors, of that many cars. For sanity sake you could (and probably should) use the array allocator in the standard library, calloc, though it isn't strictly needed. It would look something like this:

struct Cars (*ParkingLot)[10] = calloc(FloorCount, sizeof *ParkingLot);

Some find it clearer, and relish in the fact it zero-initializes the allocated memory.


On to your question, to pass a true array of arrays (as opposed to an array of pointers) your function parameter declaration is incorrect.

This:

void CarIn (struct Cars *Lot[])

declares an arbitrary-length array of struct Cars *, and indeed the above is equivalent to this:

void CarIn (struct Cars **Lot)

You want neither of those if your intent is to pass a true array of arrays (which is what your allocation is indeed acquiring). Instead, you want something like this:

void CarIn (struct Cars Lot[][10])

or equivalently, this:

void CarIn (struct Cars (*Lot)[10])

To pass a true array of arrays to a function, all dimensions but the most-superior must be provided in the declaration. The above declare an arbitrary length array of element-count-10 arrays of Cars.

Note: This can be done with variable-length syntax as well, if the sizing for the array of arrays inferior dimension is runtime-generated, but that doesn't seem to be your question (yet). For now, the above should get you where you want to go.

Best of luck.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
-1

struct does not create a 2D array, struct creates a structure - a 'template' that unifies different data types and stores them as an 'unity' in memory

A 2D array is created by i.e. int 2Darray[3][4] = {{0,1,2},{8,9,10,11}};

A 2D array of a structure is created by i.e. struct Cars cars2Darray[10][10];

See this http://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm

To pass a 2D array to a function as argument one possible method is to use a pointer to the 0-element of the array i.e. 2D_array[0] (if the array is named 2D_array).

In the following the pointer 'ptr' points to the 0-element of the int-array '1D_array'

int 1D_array[8]= { 1, 2, 4, 8, 16, 32, 64, 128 };
int *ptr; 
ptr = 1D_array;

The ptr = 1D_array stores the address of the 0th element of 1D_array in ptr, so ptr now points to the 0-th element of 1D_array

#include <stdio.h>
#include <stdlib.h>

int main(void) {
   int 1D_array[8]= { 1, 2, 4, 8, 16, 32, 64, 128 };
   int *ptr;
   int i;

   ptr = 1D_array;
   printf("The value, *ptr points to, is %d\n", *ptr);
   printf("When applying *ptr+1 it points to %d\n", *(ptr+1));
   printf("*(ptr+3) = %d\n", *(ptr+3));
   printf("\n all elements of 1D_array : \n");
   for(i=0; i<8; i++)
      printf("element[%d]=%d \n", i, *(ptr+i));
   return EXIT_SUCCESS;
}

As ptr now points to the 0th element of 1D_array you can use it to pass the array to a function.

For how to pass a 2D array to a function there are 3 methods (2 using pointers) see this Correct way of passing 2 dimensional array into a function

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • As you can see, I need to create a 2D array of structs, and since I'm using `malloc` in order to create it, I have a pointer to that array already. I already managed to print using the function, all I needed is to add `&` But now I have another problem.. – Voxito Apr 09 '16 at 08:08
  • i did not get that this is a **dynamic** method of memory allocation to create array `struct Cars (*ParkingLot)[10] = malloc(FloorCount * sizeof *ParkingLot);` – ralf htp Apr 09 '16 at 09:31