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

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

  1. 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.
     
    , Aug 17, 2013
    #1
    1. Advertising

  2. Guest

    On Saturday, August 17, 2013 9:10:06 AM UTC-4, wrote:
    > 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.


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

  3. Ike Naar Guest

    On 2013-08-17, <> wrote:
    > what does this mean?


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

    aFunction(const char * array[])

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


    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. writes:

    > what does this mean?


    First, it means the same as aFunction( const char **array ). The []
    notation can just be confusing.

    > the array being passed in, is not allowed to be modified inside the
    > function?


    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.
     
    Ben Bacarisse, Aug 17, 2013
    #4
  5. Guest

    On Saturday, August 17, 2013 3:06:49 PM UTC-4, Ike Naar wrote:
    > On 2013-08-17, <> wrote:
    >
    > > what does this mean?

    >
    >
    >
    > Please include the question in the body of the message, not only in the subject.
    >
    > So, we're talking about
    >
    >
    >
    > aFunction(const char * array[])
    >
    >
    >
    > > the array being passed in, is not allowed to be modified inside the function?

    >
    > > or it means nothing. or what?

    >
    >
    >
    > 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 */


    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?
     
    , Aug 18, 2013
    #5
  6. Ike Naar Guest

    On 2013-08-18, <> wrote:
    > 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[] )


    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.

    > how did you know that... char const * const *a ...would work?


    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. Guest

    On Sunday, August 18, 2013 2:38:38 AM UTC-4, Ike Naar wrote:
    > On 2013-08-18, <> wrote:
    > > 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[] )


    > 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.
    >
    > > how did you know that... char const * const *a ...would work?


    > 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 *).


    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?
     
    , Aug 27, 2013
    #7
  8. James Kuyper Guest

    On 08/27/2013 03:35 AM, wrote:
    ....
    > 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?


    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
     
    James Kuyper, Aug 27, 2013
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lovecreatesbeauty
    Replies:
    1
    Views:
    1,105
    Ian Collins
    May 9, 2006
  2. Javier
    Replies:
    2
    Views:
    593
    James Kanze
    Sep 4, 2007
  3. ...vagrahb
    Replies:
    1
    Views:
    302
    Vidar Hasfjord
    May 6, 2008
  4. ctj951
    Replies:
    4
    Views:
    317
    Leandro Melo
    Nov 14, 2008
  5. ctj951
    Replies:
    4
    Views:
    295
    Keith Thompson
    Nov 14, 2008
Loading...

Share This Page