Thread: char pointer to pointer question

  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 11: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,534
    This won't even compile. Show us the version you were able to compile with.
    Last edited by kermit; 01-10-2009 at 11:56 AM.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    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 12:03 PM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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