Thread: Usage of double pointers

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    50

    Usage of double pointers

    Is there any difference between the following two initializations.


    Code:
    case1)char **ch = {"abc","def"};
    
    
    case2)char *ch[] = {"abc","def"};
    I have tried this just to understand usage of double pointers.
    I have seen many places using double pointers, actually my doubht here is we can use single pointer to point specific memory location and if we need to store pointers then we can use array of pointers (*arr[]), in which scenarios we will use double pointers.


    I can print the strings in "case2" using "printf("%s %s", ch[0],ch[1])". How can we print the strings in "case1", when tried the same then it is giving segfault.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Well, take a look at these warnings:
    Code:
    test.c: In function ‘main’:
    test.c:5:16: warning: initialization from incompatible pointer type [-Wincompatible-pointer-types]
      char **ch = { "abc", "def" };
                    ^
    test.c:5:16: note: (near initialization for ‘ch’)
    test.c:5:23: warning: excess elements in scalar initializer
      char **ch = { "abc", "def" };
                           ^
    test.c:5:23: note: (near initialization for ‘ch’)
    I haven't come across this problem before, but it seems you can't directly assign the address of an initialization list to a variable, as it probably doesn't have one yet.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    50
    Thanks for the reply.
    So we can't initialize a double pointers?
    Can you guide to understand the usage of double pointers.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by krkr View Post
    So we can't initialize a double pointers?
    You can, but not like this. For example, you can do:
    Code:
    const char *ch2[] = { "abc", "def" }; // ch2 has to be stored somewhere, of course
    const char **ch = ch2;
    or if you'd like to go the dynamic allocation route:
    Code:
    const char **ch = malloc(sizeof(char*)*2);
    ch[0] = "abc";
    ch[1] = "def";
    ...
    free(ch);
    or you could even scratch this double-pointer idea and try something else, like a multi-string:
    Code:
    const char *ch = "abc\0def\0";
    // This needs extra code for some things though
    Last edited by GReaper; 08-24-2017 at 06:44 AM.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-29-2015, 01:15 PM
  2. double pointers
    By lexitron in forum C Programming
    Replies: 2
    Last Post: 11-09-2011, 01:40 AM
  3. Replies: 6
    Last Post: 07-21-2008, 06:04 AM
  4. Pointers (temporary usage, not as alias)
    By myle in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2007, 12:12 PM
  5. Double Pointers
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 04-13-2002, 03:57 AM

Tags for this Thread