when a function call has: aFunction( const char * array[] )

Discussion in 'C Programming' started by gdotone, Aug 17, 2013.

  1. gdotone

    gdotone Guest

    what does this mean?

    the array being passed in, is not allowed to be modified inside the function?
    or
    it means nothing.
    or
    what?

    Thanks everyone. my reading through the draft of the spec. is slow going.

    thanks again.

    g.
     
    gdotone, Aug 17, 2013
    #1
    1. Advertisements

  2. gdotone

    gdotone Guest

    aFunction( const char * arrray[] )
    or does it mean the pointers to the array are constant and not the array itself?
     
    gdotone, Aug 17, 2013
    #2
    1. Advertisements

  3. gdotone

    Ike Naar Guest

    Please include the question in the body of the message, not only in the subject.
    So, we're talking about

    aFunction(const char * array[])
    Inside aFunction, array points to the first element of an
    array of pointers to arrays of read-only characters.
    array itself can be modified, array can be modified, but array[j] can't.
    Here's an example program:

    /* begin example.c */

    #include <stdio.h>

    static void display(char const * const *a)
    {
    while (*a != NULL) puts(*a++);
    puts("");
    }

    static void aFunction(const char *array[])
    {
    /* array[1][2] = 'a'; */ /* not allowed */
    display(array);
    array[1] = "nifty"; /* allowed */
    display(array);
    char const * othertext[] = {"elvis", "lives", NULL};
    array = othertext; /* allowed */
    display(array);
    }

    int main(void)
    {
    char const *text[] = {"the", "quick", "brown", "fox", NULL};
    aFunction(text);
    return 0;
    }

    /* end example.c */
     
    Ike Naar, Aug 17, 2013
    #3
  4. First, it means the same as aFunction( const char **array ). The []
    notation can just be confusing.
    Inside the function

    array is of type const char ** (pointer to pointer to const char)
    *array is of type const char * (pointer to const char)
    **array is of type const char

    Only the last type is const-qualified. All the other types mean the
    object can be modified without undefined behaviour. Thus (using array
    index notation for a change)

    array = malloc(42 * sizeof *array); // OK
    array[0] = "abc"; // OK
    array[1][1] = 'x'; // Boom!
     
    Ben Bacarisse, Aug 17, 2013
    #4
  5. gdotone

    gdotone Guest



    in example.c you used
    static void display(char const * const *a)

    i see it works the same as
    static void display(char const *a[] )

    how did you know that... char const * const *a ...would work?
     
    gdotone, Aug 18, 2013
    #5
  6. gdotone

    Ike Naar Guest

    static void display(char const * const *a)

    declares that display will not modify *a, and not modify **a, where

    static void display(char const *a[])

    only declares that display will not modify **a.
    If a function has a parameter of type (T const *), the caller
    of the function is allowed to pass an argument of type (T *).
    In the example above, T is (char const *).
     
    Ike Naar, Aug 18, 2013
    #6
  7. gdotone

    gdotone Guest

    char q = 'L';

    const char * const a_Ptr = &q;

    this reads as : a_Ptr is a const pointer to a char const
    here a_Ptr can't be assigned to point to another memory address and the data,
    a char const, can't be changed

    char const * const *a_NextPtr;
    would read as: a_NextPtr is a pointer constant pointer to a constant char?
    if that's the case what does that mean? a pointer to an array of char(s) that can't
    be changed?
     
    gdotone, Aug 27, 2013
    #7
  8. gdotone

    James Kuyper Guest

    On 08/27/2013 03:35 AM, wrote:
    ....
    C declarations are based upon the principle that the declaration of an
    identifier reflects the way that it is used. There are additional
    principles, and exceptions to that principle, but it is the most
    fundamental principle that's relevant.

    In this case, what that means is the for any given '*', the '*' and
    everything following (after dropping qualifiers) is a model of a C
    expression involving the identifier being declared. Everything to the
    left of the '*' is the type of that expression. This rule requires
    modification in some circumstances, and doesn't apply in a few others,
    but it applies here.

    The first '*' means is that the expression **a_NextPtr has the type
    "char const" - so it is NOT a modifiable lvalue.
    The second '*' means that the expression *a_NextPtr has the type "char
    const* const" - the rightmost "const" indicates that this expression is
    also not a modifiable lvalue.
    a_NextPtr itself has the type "char const*const*"; the fact that there
    is no 'const' after the second '*' indicates that a_NextPtr itself can
    be modified.

    So a_NextPtr is pointer to a const pointer to const char.
     
    James Kuyper, Aug 27, 2013
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.