3

I'm a noob so don't be hard on be.

Instead of something like this;

char string[NUM OF STRINGS][NUM OF LETTERS];

Is it possible to dynamically allocate how many strings will be in the array with malloc just like when you dynamically allocate memory for char pointer? Something like this:

int lines;
scanf("%d", &lines);
char *string[NUM OF LETTERS]
string = malloc(sizeof(char) * lines);

I tried it but it doesn't work; There must be something I'm doing wrong. The other solution I thought of was:

int lines;
scanf("%d", &lines);
char string[lines][NUM OF LETTERS];

but I want to know if that's possible using malloc.

Dj Doina
  • 31
  • 1
  • 1
  • 3

5 Answers5

4

You can also use malloc for each word, like this

char **array;
int    lines;   
int    i;   

while (scanf("%d", &lines) != 1);

array = malloc(lines * sizeof(char *));
if (array != NULL)
{
    for (i = 0 ; i < lines ; ++i)
    {
        int numberOfLetters;

        while (scanf("%d", &numberOfLetters) != 1);
        array[i] = malloc(numberOfLetters);
    }
}    

where numberOfStrings and lengthOfStrings[i] are integers that represent the number of strings you want the array to contain, an the length of the ith string in the array respectively.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
  • Worth noting is that this isn't equivalent to `char string[NUM OF STRINGS][NUM OF LETTERS];`. This version isn't contiguous. Instead OP might (or probably not, in this case) want to do a single malloc of numberOfStrings*numberOfLetters. – tux3 Jan 11 '15 at 02:04
  • why there no need for typecasting? malloc return void* but in this case there is no typecasting done – Mayank Mar 05 '16 at 06:16
  • @Mayank Because in [tag:c] you don't need to cast from `void *`. In [tag:c] *unlike [tag:c++]*, `void *` is converted to any pointer type without casting. [Read this for more](http://stackoverflow.com/a/605858/1983495) – Iharob Al Asimi Mar 05 '16 at 07:11
2

You have two methods to implement this.

First is more complicated, cause it requires the allocation of memory for array of pointers to strings, and also allocation of memory for each string.

You can allocate the memory for entire array:

char (*array)[NUM_OF_LETTERS]; // Pointer to char's array with size NUM_OF_LETTERS
scanf("%d", &lines);
array = malloc(lines * NUM_OF_LETTERS);
. . .
array[0] = "First string\n";
array[1] = "Second string\n";
// And so on;

A disadvantage of the second method is that NUM_OF_LETTERS bytes are allocated for each string. So if you has many short strings, the first method would be better for you.

Mark Shevchenko
  • 7,937
  • 1
  • 25
  • 29
2

In case you want contiguous memory allocation:

char **string = malloc(nlines * sizeof(char *));
string[0] = malloc(nlines * nletters);
for(i = 1; i < nlines; i++)
    string[i] = string[0] + i * nletters;  

For more detailed explanation: Read FAQ list · Question 6.16.

haccks
  • 104,019
  • 25
  • 176
  • 264
0
int lines;
scanf("%d", &lines);
char (*string)[NUM OF LETTERS]
string = malloc(sizeof(*string) * lines);
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • You should explain why this works and `*string[NUM OF LETTERS]` doesn't. – Iharob Al Asimi Jan 11 '15 at 02:09
  • It is funny to use the number of characters in the number of pointers. – BLUEPIXY Jan 11 '15 at 02:12
  • So when I was using this `char *string[NUM OF LETTERS]` NUM OF LETTERS is actually the number of pointers? I'm confused. @BLUEPIXY – Dj Doina Jan 11 '15 at 02:17
  • @Dj Doina Yes, it is. `type *var[NUM]` means array of `NUM` pointers to `type`, cause [] has a priority. `type (*var)[NUM]` means pointer to array of `NUM` elements of `type`, cause () has a priority. – Mark Shevchenko Jan 11 '15 at 02:19
-1
char **ptop;
int iStud;
int i;
printf("Enter No. of Students: ");
scanf("%d",&iStud);

ptop=(char **) malloc(sizeof(char)*iStud);
flushall();


for(i=0;i<iStud;i++)
{
        ptop[i]=(char *) malloc(sizeof(char)*50);
        gets(ptop[i]);
}


for(i=0;i<iStud;i++)
{
    puts(ptop[i]);
}
free(ptop);