Use of 'const' with pointers to pointers

This is a discussion on Use of 'const' with pointers to pointers within the C Programming forums, part of the General Programming Boards category; I've gotten myself into a little confusion regarding the use of const and pointers to pointers of const arrays. Take ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Use of 'const' with pointers to pointers

    I've gotten myself into a little confusion regarding the use of const and pointers to pointers of const arrays. Take for example this function:

    Code:
    void list_alpha(const struct seat ar[])
    {
        const struct seat** pfull;
        int nFull = MAXSEATS - num_empty(ar);
        int i, j, count;
        const struct seat* temp;
        char iname[NAMESIZE * 2];
        char jname[NAMESIZE * 2];
    
        pfull = (struct seat**) malloc(nFull * sizeof(struct seat*));
        for (i = 0, count = 0; i < MAXSEATS; i++)
            if (ar[i].taken)
            {
                pfull[count] = &(ar[i]);
                count++;
            }
    
        for (i = 0; i < (count - 1); i++)
            for (j = i + 1; j < count; j++)
            {
                strcpy(iname, pfull[i]->lname);
                strcat(iname, pfull[i]->fname);
                strcpy(jname, pfull[j]->lname);
                strcat(jname, pfull[j]->fname);
                if (strcmp(iname, jname) > 0)
                {
                    temp = pfull[i];
                    pfull[i] = pfull[j];
                    pfull[j] = temp;
                }
            }
    
        for (i = 0; i < count; i++)
            printf("%d: %s, %s\n", pfull[i]->id, pfull[i]->lname, pfull[i]->fname);
    
        free(pfull);
        return;
    }
    The call to free() at the end brings forth the warning : 'function' : different 'const' qualifiers - and I just can't get my pretty little head around the reason why. Any help?

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    free() takes a non-const pointer, plain and simple. The assumption is that you would never have dynamically allocated a const array since the only purpose in doing that is to write data into it. (I know this argument is weak and not even completely correct, but it's the only reason I can imagine why free() would have been made to take a non-const)

    You can still maintain a const pointer to the array for any code which doesn't need to modify it, but as far as allocating/deallocating, it should not be const. Either that, or cast away the const-ness when you call free(), but I'd recommend the former solution.
    Last edited by brewbuck; 03-27-2009 at 12:01 AM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    My guess is that you should change this:
    Code:
    pfull = (struct seat**) malloc(nFull * sizeof(struct seat*));
    to:
    Code:
    pfull = malloc(nFull * sizeof(*pfull));
    Quote Originally Posted by brewbuck
    free() takes a non-const pointer, plain and simple.
    Yes, but pfull is a non-const pointer to a pointer to const, unless I read it wrongly.
    Last edited by laserlight; 03-27-2009 at 12:02 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 Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Point taken! I un-const'd pfull and temp and the warning is no more. Thanks.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    Quote Originally Posted by laserlight View Post
    My guess is that you should change this:
    Code:
    pfull = (struct seat**) malloc(nFull * sizeof(struct seat*));
    to:
    Code:
    pfull = malloc(nFull * sizeof(*pfull));

    Yes, but pfull is a non-const pointer to a pointer to const, unless I read it wrongly.
    I read it as "a pointer to a pointer to a struct seat, which is const." At any rate, the compiler doesn't like it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Quote Originally Posted by laserlight View Post
    My guess is that you should change this:
    Code:
    pfull = (struct seat**) malloc(nFull * sizeof(struct seat*));
    to:
    Code:
    pfull = malloc(nFull * sizeof(*pfull));
    But if I don't cast pfull, how can I use array notation on it? My understanding was that without the cast, malloc returns pointer-to-void?

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by Sharke View Post
    But if I don't cast pfull, how can I use array notation on it? My understanding was that without the cast, malloc returns pointer-to-void?
    it is not important what malloc returns - important where you store the value - I mean what it the type of pfull

    if the type is incorrect - cast while assigning value will not help later access it. Value has no type - variable has type
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    I see! Another point taken.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template has already been defined
    By Elysia in forum C++ Programming
    Replies: 19
    Last Post: 04-14-2009, 10:17 AM
  2. The so annoying LNK2005... please help!
    By Mikey_S in forum C++ Programming
    Replies: 14
    Last Post: 02-01-2009, 03:22 AM
  3. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  4. fatal error LNK1104
    By DMH in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2005, 02:46 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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