Thread: Dynamic array of pointers

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    29

    Dynamic array of pointers

    Hi. I'm learning about pointers and as an exercise I'm trying to create a program that will store an unknown number of pointers to strings that will be input by the user in an array of pointers.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        char input[100];
        char **strings;
    
        do {
            printf("Type something: ");
            fgets(input, sizeof(input), stdin);
    
            /* I don't know what to do now */
        } while (strcmp(input, "exit") != 0);
    
        /* free the memory that was allocated */
    
        return 0;
    }
    If I was using something like
    Code:
    char *strings[5000];
    then I'd know what to do but using **strings is confusing me. Anyone can give me a hint? Thanks.

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    **strings just means that strings is a pointer which can hold the memory address of some other pointer.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Start with
    char **strings = NULL;

    Then keep two counters
    int numStrings = 0, maxStrings = 0;

    Then you do this
    Code:
    if ( numStrings == maxStrings ) {
      void *temp = realloc ( strings, (maxStrings+10) * sizeof *strings );
      if ( temp != NULL ) {
        strings = temp;
        maxStrings += 10;
      } else {
        // handle error
      }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    Code:
    if ( numStrings == maxStrings ) {
      /* after this, I'll be able to store 10 strings in variable strings, right? */
      void *temp = realloc ( strings, (maxStrings+10) * sizeof *strings );
      if ( temp != NULL ) {
        strings = temp;
        maxStrings += 10;
      } else {
        // handle error
      }
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes.

    And you increment numStrings after each one you add.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    Is this correct?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        char *p = NULL;
        char linha[100];
        char **strings = NULL;
        int c = 0;
        int nstrings = 0;
        int maxstrings = 0;
        void *temp = NULL;
    
        for (;;) {
            printf("Type something or exit to exit: ");
            fgets(linha, sizeof(linha), stdin);
    
            if ((p = strchr(linha, '\n')) != NULL)
                *p = '\0';
            else
                while ((c = getchar()) != '\n' && c != EOF);
    
            if (strcmp(linha, "exit") == 0)
                break;
    
            if (nstrings == maxstrings)
                temp = realloc(strings, (maxstrings + 10) * sizeof(*strings));
            if (temp) {
                strings = (char **)temp;
                maxstrings += 10;
            }
            p = malloc(strlen(linha) + 1);
            if (p)
                strcpy(p, linha);
            else
                break;
            strings[nstrings++] = p;
        }
    
        --nstrings;
    
        for (; nstrings >= 0; nstrings--) {
            printf("%s\n", strings[nstrings]);
            free(strings[nstrings]);
        }
    
        free(strings);
    
        return 0;
    }
    Thanks.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You should check the return result of fgets(), and break if it's NULL
    You should also break if your realloc fails (like you break if your malloc fails)
    The cast (char**) is redundant.

    Apart from those easy to fix issues, it's looking good
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Sep 2005
    Posts
    29
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
        char *p = NULL;
        char linha[100];
        char **strings = NULL;
        int c = 0;
        int nstrings = 0;
        int maxstrings = 0;
        void *temp = NULL;
    
        for (;;) {
            printf("Type something or exit to exit: ");
            if (fgets(linha, sizeof(linha), stdin) == NULL)
                break;
    
            if ((p = strchr(linha, '\n')) != NULL)
                *p = '\0';
            else
                while ((c = getchar()) != '\n' && c != EOF);
    
            if (strcmp(linha, "exit") == 0)
                break;
    
            if (nstrings == maxstrings)
                temp = realloc(strings, (maxstrings + 10) * sizeof(*strings));
            
            if (temp) {
                strings = temp;
                maxstrings += 10;
            }
            else
                break;
    
            p = malloc(strlen(linha) + 1);
            if (p)
                strcpy(p, linha);
            else
                break;
            strings[nstrings++] = p;
        }
    
        --nstrings;
    
        for (; nstrings >= 0; nstrings--) {
            printf("%s\n", strings[nstrings]);
            free(strings[nstrings]);
        }
    
        free(strings);
    
        return 0;
    }
    Just for testing purposes, I entered all the letters of the alphabet, one letter per line. Here's my output:
    Code:
    Type something or exit to exit: a
    Type something or exit to exit: b
    Type something or exit to exit: c
    Type something or exit to exit: d
    Type something or exit to exit: e
    Type something or exit to exit: f
    Type something or exit to exit: g
    Type something or exit to exit: h
    Type something or exit to exit: i
    Type something or exit to exit: j
    Type something or exit to exit: k
    Type something or exit to exit: l
    Type something or exit to exit: m
    Type something or exit to exit: n
    Type something or exit to exit: o
    Type something or exit to exit: p
    Type something or exit to exit: q
    Type something or exit to exit: r
    Type something or exit to exit: s
    Type something or exit to exit: t
    Type something or exit to exit: u
    Type something or exit to exit: v
    Type something or exit to exit: w
    Type something or exit to exit: x
    Type something or exit to exit: y
    Type something or exit to exit: z
    Type something or exit to exit: exit
    z
    y
    x
    w
    v
    u
    t
    s
    r
    q
    p
    o
    n
    m
    l
    k
    j
    i
    h
    g
    f
    e
    d
    c
    A little window pops-up asking if I want to send the error to Microsoft. I can't see the error but I think it has something to do with memory freeing. Can someone help me find the problem, please?

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Start making braces compulsory in your code
    Code:
        if (nstrings == maxstrings) {
          temp = realloc(strings, (maxstrings + 10) * sizeof(*strings));
          if (temp) {
            strings = temp;
            maxstrings += 10;
          } else {
            break;
          }
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers to an array pointers
    By onebrother in forum C Programming
    Replies: 2
    Last Post: 07-28-2008, 11:45 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Syntax for constant array of array pointers
    By BMintern in forum C Programming
    Replies: 4
    Last Post: 05-14-2008, 08:21 AM
  4. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM