1

I am new in whole C programming thing (comming from Java), and honestly its really confusing. Ok to the problem I am trying to allocate contigous chunk of data for my 2D array of strings (Guessing its something like 3D array??). All I have is this i believe contiguous allocation for Array of strings? Can someone help me out with 2D array please?

And yes I know size before running the program, its defined so ROWS for rows, COLS for columns and NAME for length of string.

char **people = malloc(COLS * sizeof(char *));

people[0] = malloc(COLS * NAME);
for(int i = 1; i < COLS; i++)
    people[i] = people[0] + i * NAME;
Pauli
  • 388
  • 4
  • 17
  • Your code never uses ROWS. Is that intentional? – ruakh Nov 12 '16 at 22:39
  • I forget to mention that this code should allocate contiguous data for array of strings. for 1D array not 2D i can't figure out 2D thing that is what i need help with, and thats the reason i never used ROWS. – Pauli Nov 12 '16 at 22:43
  • `char (*people)[COLS][NAME] = malloc(ROWS * sizeof *people);` should do the job. – mch Nov 12 '16 at 23:30

3 Answers3

1

If you actually know the size of the array before running the program, you don't need to dinamically allocate the memory with malloc, you could create a 2D static array. In your case, as it is a 2D array of strings, it could be declared as char * array[ROWS][COLS], and then you could asign a string to a specific element this way: array[nrow][ncol]="Your String".

Sizigia
  • 582
  • 5
  • 10
  • 1
    That would not actually be contiguous. It sounds like the OP wants `char array[ROWS][COLS][NAME]`, and then `strcpy(array[nrow][ncol], "Your String")`. – ruakh Nov 12 '16 at 23:17
  • Sizigia sorry i need contiguous memory. Ruakh omg man thanks i somehow miss that way of approach i get so caught up in dynamically allocating string that i miss strcpy... good things is i have much better understanding for pointers and arrays from now :D thanks – Pauli Nov 12 '16 at 23:24
  • (Oh, but: +1 for the suggestion to use static memory. Usually we think of heap vs. stack, and of course the stack is sometimes has much more restrictive size limits than the heap; but static allocation is a good alternative in many cases.) – ruakh Nov 12 '16 at 23:30
  • @Pauli no problem, I thought that you just wanted to allocate contiguous memory for the array itself and not specifically for the string elements too, as you were trying to use pointers on your code. – Sizigia Nov 13 '16 at 00:52
1

C, unlike Java, actually has a concept of multidimensional arrays; so unless there's a specific reason you want a char * * *, you might prefer to write:

char (*people)[COLS][NAME] = malloc(ROWS * sizeof(*people));

which sets people to be a pointer to the first of ROWS dynamically-allocated two-dimensional character arrays.

Due to pointer "decay", where an expression of array type will double as a pointer to the first element of the array, you can use people very much as if it were a char * * *; for example, people[3][4] will point to the string in row 3, column 4. The only restriction is that you can't write something like people[3][4] = ... to suddenly change what string to point to. But it sounds like you don't want to do that, anyway?


Note: the above is assuming that you are intentionally using dynamic memory allocation. However, I do recommend you consider Sizigia's suggestion to use static memory, which is the same sort of storage as is used for global variables. If you write something like

static char people[ROWS][COLS][NAME];

then the memory will be allocated just once, at the start of the program, and reused by all calls to the function that declares it.

ruakh
  • 175,680
  • 26
  • 273
  • 307
1

You can define a char * using typedef so it's better for you to understand the code. Then all you have to do is to dynamically allocate a 2D array of your defined type (in the example below, I defined it as a "string"):

 typedef char * string;
 string ** people;
 people = malloc(ROWS * sizeof(string));
 for(int i = 0; i < ROWS; i++){
    people[i] = malloc(COLUMNS * sizeof(char));
 }

You can access it using the normal array sintax, people[i][j].

Augusto
  • 89
  • 5