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

G

gdotone

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

gdotone

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?
 
I

Ike Naar

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

Ben Bacarisse

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!
 
G

gdotone

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?
 
I

Ike Naar

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

gdotone

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?
 
J

James Kuyper

On 08/27/2013 03:35 AM, (e-mail address removed) 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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top