0

I am doing some coding at the moment using C language just to develop my C skills. What I am doing is to store words in an allocated dynamic memory but having some difficulties with **pointer...

For example,

while ((ch = getchar()) != EOF)

if I type abcd efgh, the characters, "abcd" should be stored in ptr[0][i] and the second one, "efgh" should be stored in ptr[1][i] and this should be done through looping.

I want to do it by initializing,

char **ptr = (char**)malloc(sizeof(char*)*n);

is this possible??

Any help would be very thankful!

lurker
  • 56,987
  • 9
  • 69
  • 103
  • Not sure what you mean by the `i` in your expressions. I assume you mean you want to store strings at locations `ptr[0]`, `ptr[1]`, etc. You're starting out almost right. [You don't want to cast `malloc` return](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). Also, once you've allocated `ptr`, you're going to need to allocate each string pointer, *e.g.*,. `ptr[0] = malloc(sizeof(char) * string_buffer_length)`, etc. – lurker Jun 23 '17 at 16:40
  • Yes, but that allocates n **pointers** that point to nothing. You must then allocate a character buffer for each pointer. – Dave S Jun 23 '17 at 16:41

2 Answers2

0

Here is an example of storing some strings in a dynamic array (char **).

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

int main(void)
{
    char    **strings = NULL;
    size_t  nb_strings = 3;
    size_t  strings_length = 10;

    strings = malloc( sizeof(*strings) * nb_strings );
    // Now you can store 3 pointers to char

    for (size_t index = 0 ; index < nb_strings ; index++)
        strings[index] = malloc( sizeof(**strings) * strings_length );
        // Every pointer points now to a memory area of 10 bytes

    for (size_t index = 0 ; index < nb_strings ; index++)
    {
        strings[index][0] = '\0';
        strncat( strings[index], "string", strings_length - 1 );
        // You can store some strings now
    }

    for (size_t index = 0 ; index < nb_strings ; index++)
        printf("strings[%zu] = %s.\n", index, strings[index]);
    // You can check

    for (size_t index = 0 ; index < nb_strings ; index++)
        free(strings[index]);

    free(strings);
    // Do not forget to free

    return (0);
}
Gam
  • 684
  • 1
  • 8
  • 17
0

You need to understand realloc(). You've got two levels of list going, you've got a list of words which must expand as new words are input, and each word is a list of characters.

Start off with an empty list of words

 char **words = 0;
 int Nwords = 0;

And an empty word

 char *word = 0;
 int wordlen = 0;

The as you have done, our main loop is a characterwise read of the input

 while( (ch = getchar()) != EOF)
 {
    /* logic here */
 } 

So what's the logic?

 while( (ch = getchar()) != EOF)
 {
    if(!isspace(ch))
    {
       /* add letter to word */
    }
    else
    {
       if(wordlen > 0)
       {
          /* add word to word list */
       }
    }
 }

I could fill out the comments but that would be doing it for you. You grow the buffers with realloc() for each new entry.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18