char pointer to pointer question

This is a discussion on char pointer to pointer question within the C Programming forums, part of the General Programming Boards category; Hi everyone, I've been playing around with pointer to pointers recently and stumbled upon a weird problem I can't figure ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    char pointer to pointer question

    Hi everyone,

    I've been playing around with pointer to pointers recently and stumbled upon a weird problem I can't figure out.

    Code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main ()
    {
            char **test;
            char *str1 = "string 1";
            char *str2 = "string 2";
            char *str3 = "string 3";
            int i = 0;
    
            test = malloc(3 * sizeof(char *));
    
            test[0] = str1;
            test[1] = str2;
            test[2] = str3;
    
            while (*test++) {
                    printf("%d\n",i);
                    ++i;
            }
    
            return 0;
    }

    Compiling with "gcc -O2 -Wall -pedantic -ansi -g test.c -o test" which results in:

    0
    1
    2
    3

    Even though there are only 3 char pointers assigned, shouldn't it stop if i == 2 ?
    Changing the malloc to malloc(4 * sizeof(char *)) seems to be fixing the problem, but that can't be right, making room for four char pointers but only using three?

    Also if I'm assigning one, two, four or more char pointers to test it counts correctly. It only fails when assigning 3 pointers to test.

    Can someone please point out to me what I'm missing here?

    salt shaker
    Last edited by Salt Shaker; 01-10-2009 at 10:57 AM. Reason: Ah yes my bad, had a little typo, st3 instead of str3 :)

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    This won't even compile. Show us the version you were able to compile with.
    Last edited by kermit; 01-10-2009 at 10:56 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    The problem is that you loop until *test is a null pointer, but the test array has no null pointers, hence you access the array out of bounds, resulting in undefined behaviour. Allocating space for 4 pointers happens to work probably because the fourth pointer is incidentally a null pointer, but more accurately:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <stddef.h>
    
    int main()
    {
        const char **test;
        const char *str1 = "string 1";
        const char *str2 = "string 2";
        const char *str3 = "string 3";
        int i = 0;
    
        test = malloc(4 * sizeof(char *));
        if (test) {
            test[0] = str1;
            test[1] = str2;
            test[2] = str3;
            test[3] = NULL;
    
            while (*test++) {
                printf("%d\n", i);
                ++i;
            }
    
            free(test);
        } else {
            printf("Unable to allocate memory.\n");
        }
    
        return 0;
    }
    Notice that I assign NULL to test[3], hence the loop surely works. I also include <stdio.h> and <stddef.h> for printf and NULL respectively. I deal with const char* instead of char* since the program deals with string literals, and string literals must not be modified. I also used free() when test is no longer to be used, and checked the return value of malloc().
    Last edited by laserlight; 01-10-2009 at 11:03 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    Thanks laserlight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Basic things I need to know
    By maxorator in forum C++ Programming
    Replies: 53
    Last Post: 10-15-2006, 04:39 PM
  5. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21