0

i was wondering how do i create a ragget 2d string(char*)array in c . (my example code looks like this)

    int j = 1;
    char **array = null;
   ... read string...
   *array = (char *) realloc(sizeof(char *) * j);
   j++;
(sirAvertisment[j] = (char **) realloc((*sirAvertisment)[j],sizeof(char *) * strlen(somearray[i]))

for example i want it

  1. A L F A B E T
  2. A P P L E
  3. K I D
  4. S E M A P H O R E S

but with pointers so when i read 1 row of a matrix i get the word complete

Hex
  • 3
  • 3
  • 1
    I think you should post a bit more of your code as the fragment you have posted is too limited for us to help. I imagine your entire program at this point isn't too large and you can/should post it all so we can download/compile/link/run it if we choose. Also, add a small input data file that illustrates the problem. See: https://stackoverflow.com/help/how-to-ask – Craig Estey Jan 25 '20 at 21:55
  • `*array = ...` dereferences something that has not been allocated before. – Stephan Lechner Jan 25 '20 at 21:59
  • You've read [ask], now red [mcve]. – jwdonahue Jan 25 '20 at 22:02
  • `realloc()` takes two arguments; you're calling it with just one. That's careless. The compiler should complain, of course. You need to create an array of pointers. For each string, you then need to allocate an array of characters and assign that to one of the array of pointers. Don't forget to free both the arrays of characters and the array of pointers. You can grow the array of pointers as needed if you don't know in advance how many pointers you need. Where are the strings coming from? A file? The user typing? – Jonathan Leffler Jan 25 '20 at 22:02
  • i read a word and store it in char somearray[LEN_MAX]; then im trying to add it to the matrix, but whene i try to increase the size of columns on the row with the size of the word i just read (*array[j] = (char **) realloc((*array)[j],sizeof(char *) * strlen(somearray[i])), i get segment fault, the problem is how do i increase the columns on each row with the exact size of the 'string' i just read – Hex Jan 25 '20 at 22:02
  • i forgot to add to the code *array = (char *) realloc(*array ,sizeof(char *) * j) – Hex Jan 25 '20 at 22:04
  • Edit the question to add the code. It's not readable and is easily missed in comments. – kaylum Jan 25 '20 at 22:04

1 Answers1

1

Your general idea is right, but you have some details wrong.

You shouldn't indirect through the pointers when assigning the allocations.

You need to add 1 to strlen() when allocating, to allow foom for the trailing null.

You shouldn't cast the result of realloc.

You should check that realloc() is successful before reassigning the original pointer.

Use malloc() when you're allocating memory for the new element you added to the array; realloc() should only be used when the pointer has already been initialized.

char **array = null;
...
char **new_array = realloc(array, sizeof(char *) * j);
if (!new_array) {
    printf("Allocation failed!\n");
    exit(1);
}
array = new_array;
array[j] = malloc(strlen(somearray[i]) + 1);
strcpy(array[j], somearray[i]);
j++;

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • so you first add row and the the size of the 'string' with malloc , but can't i just allocate the space directly in the first aray ? – Hex Jan 25 '20 at 22:18
  • You are adding storage for an additional *pointer* in `array` with `new_array` and then you allocate a block of memory to hold the string and assign the starting address to the new pointer. – David C. Rankin Jan 26 '20 at 01:35