Thread: Newbie question: char const **

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    Seattle, WA
    Posts
    3

    Question Newbie question: char const **

    I'm trying to figure out the third argument in te inotifytools_watch_recursively_with_exclude() method in inotify-tools. It's the third argument and the docs says it's type is "char const ** exclude_list". I don't know what a "char const **" is and it's not in my O'Reily book.

    Here the link to the docs: libinotifytools: inotifytools/inotifytools.h File Reference

    I was hoping someone could provide a hard coded example. IE, FileA, FileB.

    -Thanks,
    i

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    The declaration char const ** is a pointer variable which points to the same location ie a constant pointer, as in
    Code:
    char const **p;     /* p points to the same location and can't be modified */
    const char *x;      /* x is a pointer to a constant char */
    Note the position of the const keyword which makes all the difference between the two declarations

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    Seattle, WA
    Posts
    3
    I ended up getting the code running with this pretty ugly block of code:

    Code:
    	char first[100];
    	char second[100];
    	char third[100];
    	char fourth[100];
    	strcpy( first, "/c/gitshare/.AppleDouble" );
    	strcpy( second, "/c/gitshare/Network Trash Folder" );
    	strcpy( third, "/c/gitshare/Temporary Items" );
    	strcpy( fourth, "/c/gitshare/.TemporaryItems/" );
    	char *strings[] = { first, second, third, fourth, NULL };
    Is there a prettier way to do that in C?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What you have is fine though you can initialize the array strings[] using string literals directly:
    Code:
    char *strings[] = {"first item", "second item", "third item", "fourth item", "fifth item", "\0"};

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I would guess this is a sort of typo. It should read "const char**". "const" means the value of the variable will not be modified after it is declared and initialized. A "char **" is a pointer to a pointer, usually (and definately, in this case), an array of pointers to strings.

    The reason it is done this way is because the list can be of any length. The last pointer needs to be NULL. The most irritating and tedious way to deal with it would be:
    Code:
    	char source_list[4][20] = { "/hello/world", "/dir/list", "/etc", "/some/path" };
    	const char **exclude_list = malloc(sizeof(char*)*5);
    	for (i=0;i<4;i++) exclude_list[i]=source_list[i];
    	exclude_list[i] = NULL;
    If you have used the exec() family of commands, you can deal with this the same way, by including a list of strings directly in the call (as itCbitC does above). However, I think the last one should be NULL and not "\0".

    Your compiler will tell you if you have it right
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Nov 2009
    Location
    Seattle, WA
    Posts
    3
    Thanks.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by itCbitC View Post
    What you have is fine though you can initialize the array strings[] using string literals directly:
    Code:
    char *strings[] = {"first item", "second item", "third item", "fourth item", "fifth item", "\0"};
    This is incorrect. The items in your code are constant strings, which you store in non-const char. As soon as it is written to, your program will likely crash. Furthermore, the last "\0" is an empty string, identical to "", except that the former contains two 0-bytes. I expect it should read NULL, although granted, it depends on the called function what it expects.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    const char** and char const** should be the same thing. What is left of the * is the type the pointer points to.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    const char** and char const** should be the same thing. What is left of the * is the type the pointer points to.
    RIght, a pointer to a const pointer to a char would be written:

    Code:
    const char * const *ptr
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Elysia View Post
    const char** and char const** should be the same thing. What is left of the * is the type the pointer points to.
    Dang! mistook the location of "*" and yep you're right - const char * and char const * are identical; however const char * and char *const are different.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, those are different. Easy to mistake, so subtle, yet such a big difference.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by EVOEx View Post
    This is incorrect. The items in your code are constant strings, which you store in non-const char. As soon as it is written to, your program will likely crash. Furthermore, the last "\0" is an empty string, identical to "", except that the former contains two 0-bytes. I expect it should read NULL, although granted, it depends on the called function what it expects.
    Last item should be '\0' (copy and paste). The array of pointers to string literals won't let you modify its elements; however it was unspecified what was to be done with 'em.

  13. #13
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by Elysia View Post
    Yes, those are different. Easy to mistake, so subtle, yet such a big difference.
    concur!

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by itCbitC View Post
    Last item should be '\0' (copy and paste). The array of pointers to string literals won't let you modify its elements; however it was unspecified what was to be done with 'em.
    Then why does "strings[0][0] = 'a';" crash in your application? In the line of code you wrote, the string literals can't be modified. So you should store them in a CONST character array, which you didn't.

    And what should the last item be? "\0", '\0' or NULL? Arguably the last two are the same though.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is '\0' and NULL guaranteed to be the same thing? I am not so sure. Regardless, you should never do it since NULL is for pointers ONLY.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 10:00 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM