1

If I have an array, like:

char array[10][20];

then it is an array of 10 strings, each of length 20. If I make a similar array with malloc:

char **dyn_array = (char **) malloc(10 * sizeof(char *));

then how do I access the members? For example, if I had a loop like:

int limit, count=0;
printf("Enter the limit (max 10): ");
scanf("%d", &limit);
fflush(stdin);
while(count < limit)
{
    int index = 0;
    printf("\nEnter characters: ");
    fgets(array[count], sizeof(array[count]), stdin);
    fflush(stdin);
    printf("\nYou typed: ");
    while(array[count][index] != '\n' && 
        array[count][index] != '\0')
    {
        printf("%c", array[count][index]);
        index += 1;
    }
    count += 1;
}

could I replace array with dyn_array in every spot and have it be the same? What changes would I have to make? I know that the way these two kinds of arrays work is similar, but I just can't wrap my head around it.

nattynerdy
  • 411
  • 1
  • 5
  • 9
  • [`fflush(stdin);` is undefined behavior, don't do it.](https://stackoverflow.com/a/38325926/2173917) – Sourav Ghosh Oct 18 '17 at 12:42
  • one side note: Dont flush the stdin its UB. – danglingpointer Oct 18 '17 at 12:42
  • What's a better way to clear the input buffer between inputs? The answer on that other question, unfortunately, didn't provide an alternative for clearing the input buffer. – nattynerdy Oct 18 '17 at 12:49
  • A `char array[10][20]` is a two dimensional array (with 200 elements). A `char **dyn_array` is a pointer to a pointer to `char`. The latter can be used like a two dimensional array as subscript operators work for pointers as well as for arrays. However, the `char **dyn_array` (if proper allocated) turns strictly speaking out as one-dim. array of one-dim arrays. The rows are not anymore necessarily stored consecutively (which may or may not be important but is at least a fact). – Scheff's Cat Oct 18 '17 at 12:50
  • Whether the rows of a two dimensional array have to be stored consecutively (according to standard; I mean, without padding) has been hardly discussed somewhere on this site. If I only could remember the subject... – Scheff's Cat Oct 18 '17 at 12:56
  • 1
    The `char**` version is not an array, see [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). – Lundin Oct 18 '17 at 12:57
  • It is a duplicate, I think. I'll delete the question – nattynerdy Oct 18 '17 at 13:01

1 Answers1

3
char array[10][20];

then it is an array of 10 strings, each of length 20.

No it is not. It is an array of 10 arrays of 20 chars each. Remind that a c-string is just a sequence of consecutive chars terminated by a NUL one. An array of char may contains a c-string but it is not required.

If I make a similar array with malloc:

char **dyn_array = (char **) malloc(10 * sizeof(char *));

No, it is not similar, as you just created an array of 10 pointers to chars (for which your stored its address to a pointer). But these pointers (those in the array) are not initialized to anything. To obtain something similar you could have wrote:

for (int i=0; i<10; i++)
    dyn_array[i] = malloc(20*sizeof(char));

Thus, in both cases you can write array[count][index] and dyn_array[count][index].

But alas you can't write fget(dyn_array[count],sizeof(dyn_array[count],stdin); because in that case dyn_array[count] is not an array but a pointer. Remind that an array can decay to pointer, but pointer is not an array.

There is several questions about pointers vs arrays. Read.

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69