What type is address of an array

What type is address of an array

Post by MC » Sat, 06 Mar 2010 05:29:55


I have trying to find solution to this type definition. Say we have s
defined as

char s[];
and let
sp = &s;

My question is what is the exact type of s.
I think it is a pointer to a char array.
Or a pointer to a (const pointer to a char).

Hence I am guessing the type of sp to be

char * const * sp;

But the compiler gives a warning .

I know I can define sp as char** and that will work, but I wanted to
know what exactly is the type of sp and how to define variables of
that type.

Thanks,
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by ?utf-8?Q?D » Sun, 07 Mar 2010 02:52:22

MC < XXXX@XXXXX.COM > writes:

Hrm.

#include <stdio.h>

typedef char ca[];

int
main(void)
{
ca s = "foo";
ca *sp = &s;
puts(*sp);
return (0);
}

But for the life of me I cannot figure out how to write this without a
typedef.

Arrays are weird.

DES
--
Dag-Erling Smrgrav - XXXX@XXXXX.COM
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.

 
 
 

What type is address of an array

Post by Hans-Bernh » Sun, 07 Mar 2010 02:53:06


Then we don't have s defined at all, because that's not a definition of
s. Lacking a size, it can only ever be a declaration, or an error in
the source code.


That's not really your question. The type of sp is whatever you made
it, but forgot to tell us.

What you're really asking is what the type of the expression &s is, and
thus what the type of 'sp' _should_ be. Well, once s is defined, sp
will be a pointer to an array of <n> chars, for some given value of <n>:

#define LENGTH 100 /* or whatever value you need */
char s[LENGTH];
char (*sp)[LENGTH];

A good dose of typedef often helps to clarify tricky declarations:

typedef char my_array_type[LENGTH];
my_array_type s;
my_array_type *sp;


That guess is wrong.


No. It may appear to, but it won't.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by Barry Schw » Sun, 07 Mar 2010 02:53:52

On Thu, 4 Mar 2010 14:29:55 -0600 (CST), MC < XXXX@XXXXX.COM >



Since that is a syntax error everywhere except a function definition,
for sake of discussion let's replace it with
char s[10];


The exact type of s is array of 10 char. In most contexts, but NOT
the one you show above with the & operator, the expression s is
automatically converted to the address of the first element with type
pointer to element type, in this case pointer to char.


Think again


Don't think about this again.


No! Applying the & operator to an object of any type T produces a
value of type pointer to T. Since in this case T is array of 10 char,
the type of sp must be{*} pointer to array of 10 char, expressed in c
notation as
char (*sp)[10]

{*} In addition to qualified types of this unqualified type, such as
adding const, sp could also be a pointer to void. I assume that both
these exceptions are outside the intent of your question.


No it won't. Did you actually try it? You should get exactly the
same warning about incompatible pointer types.


While there are times when one wants to take the address of an array
using &, they are much rarer than the times one actually wants the
address of the first element (yes it is the same address but it has a
different type). Consider the common case of passing an array of char
to strcpy.

--
Remove del for email
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by ike » Sun, 07 Mar 2010 02:54:38

In article < XXXX@XXXXX.COM >,


Are you asking about the exact type of s, or the exact type of ps ?


s is an array of char.
ps is a pointer to an array of char.

char (*ps)[] = &s;

--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by lawrence.j » Sun, 07 Mar 2010 02:55:26


Correct.


Absolutely wrong. It is a pointer to an array, not a pointer to any
kind of pointer. An array, when used in a context that requires a
value, is *converted* into a pointer to the first element but is not
actually a pointer. And the argument of & is not a context that
requires a value. The correct type of sp is simply:

char (*sp)[];

Although it's probably worth noting that that type isn't very useful,
since there's very little you can do with an array of unknown size in C.
Typically one would either use a pointer to the element type or specify
the size of the array.
--
Larry Jones

Aw Mom, you act like I'm not even wearing a bungee cord! -- Calvin
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by MC » Sun, 07 Mar 2010 04:12:00

On Mar 5, 11:53m, Hans-Bernhard Brer < XXXX@XXXXX.COM >


> char s[LE>GTH];
> char (*sp>[L>NGTH];
>
> A good dose of typedef often helps to clarify tricky dec>ar>tions:
>
> typedef char my_array>type[LENGTH];
> > my_array_type s;
> > gt;m>_array_type *sp;
>
> > Hence I am guessin> t>e>type of sp to be
>
>
>
> > I know I can define sp as char>* >nd that will work,
>
> No. t may ap>ear t>, but it won't.
> --
> comp.lang.c.moderated - moderation address: c...@pl>thora.net -- you must
> have an appropriate newsgroups line in your header for>your mail to be seen,
> or the newsgroup name in square brackets in the subject line. orry.

Thanks a lot for your reply. It does clear up a lot of my concepts.
I have one more confusion though

say

char s[10];
then &s is of type char (*)[10];

Then why is this not a warning or error
char (*sp)[] = &s;

I am assigning to a variable of type char (*)[] a value of type char
(*)[10].

I saw this is a warning
char (*spp)[20] = &s;

Is it because sp is a pointer to an array and s "is a" array with size
10?

Thanks in advance.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by MC » Sun, 07 Mar 2010 04:12:48


Does it get converted to a pointer to char or a const pointer to a
char?
>>>>I think it is a pointer to a char array. >> >> Think again >> >>>>Or a pointer to a (const pointer to a char). >> >> Don't think about this again. >> >> >> >>>>Hence I am guessing the type of sp to be >> >>>>char * const * sp; >> >> No! pplying the & operator to an object of any type T produces a >> value of type pointer to T. ince in this case T is array of 10 char, >> the type of sp must be{*} pointer to array of 10 char, expressed in c >> notation as >> char (*sp)[10>
> {*} In addition to qualified types of this unqualified type, such a>
> adding const, sp could also be a pointer to void. assume that bot>
> these exceptions are outside the intent of your question>
> No it won't. id you actually try it? ou should get exactly th>
> same warning about incompatible pointer types>
> While there are times when one wants to take the address of an arra>
> using &, they are much rarer than the times one actually wants th>
> address of the first element (yes it is the same address but it has >
> different type). onsider the common case of passing an array of cha>
> to strcpy>
> ->
> Remove del for emai>
> ->
> comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you mus>
> have an appropriate newsgroups line in your header for your mail to be seen>
> or the newsgroup name in square brackets in the subject line. orry.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by James Kuyp » Sun, 07 Mar 2010 06:52:42


It is not a syntax error; the size of an array is optional according to
the grammar (6.7.5p1). However, it does declare an incomplete type. If s
has automatic storage duration, the behavior would therefore be
undefined due to violating a shall (6.7p7) that occurs outside of a
constraints section - that's probably what you're thinking of.

However, it would not be undefined behavior according to 6.7p7 unless
the identifier is declared with no linkage. Since no storage duration or
initializer is specified, this could only happen at file scope, in which
case it would qualify as a tentative definition (6.9.2p2), with
implicitly external linkage. As such, a compatible declaration of the
same identifier may occur farther down in the same translation unit that
either completes the type, or declares it to be explicitly extern. In
the latter case, the actual definition (which must have a complete type)
would appear in some other translation unit:

char s[];

#ifdef SAME_TRANSLATION_UNIT
char s[10];
#else
extern char s[];
#endif

Also, "As a special case, the last element of a structure with more than
one named member may have an incomplete array type; this is called a
flexible array member."

typedef struct {
size_t n;
char s[];
} counted_string;



I think it would be more appropriate to assume he was asking about one
of the three contexts where an incomplete type would not render the
behavior undefined, rather than assuming that he'd made a mistake
(though that's also a possibility).

...

Actually, in the context of a function definition, which you mentioned
above, that would be perfectly correct.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by Jasen Bett » Sun, 07 Mar 2010 17:46:08


The exact type of s is char[] // array of char
but in most places behaves like char* // pointer to char

the type of &s is char(*)[] // pointer to array of char

Note also that

char s[] // with no size specified

is only valid as the last element of a struct.


// pointer to const_pointer to char

no it's char (*sp)[]


Arrays are not pointers. they just look like pointers most of the
time, this is one of the times they do not.



--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by James Kuyp » Mon, 08 Mar 2010 04:17:56


...

Or as a function parameter, or as a tentative definition.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by ?utf-8?Q?D » Tue, 09 Mar 2010 05:27:39

Dag-Erling Smrgrav < XXXX@XXXXX.COM > writes:

Doh!

#include <stdio.h>

int
main(void)
{
char s[] = "foo";
char (*sp)[] = &s;
puts(*sp);
return (0);
}

http://www.yqcomputer.com/

I recommend reading the entire section on arrays and pointers:

http://www.yqcomputer.com/

Hell, I recommend reading the entire FAQ. It's been too long since I
last did so myself...

DES
--
Dag-Erling Smrgrav - XXXX@XXXXX.COM
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by John Bod » Fri, 12 Mar 2010 06:57:19


Here's a table that may be useful:

Declaration: T a[M];
Expression Type Decays to Note
---------- ---- --------- ----
a T [M] T * Evaluates to address of a[0]
&a T (*)[M] n/a Evaluates to address of a

Note that the address of a[0] and the address of a are the same
*value* (the address of the array is the same as the address of the
first element of the array), but the types are different.

2D arrays:

Declaration: T b[M][N];
Expression Type Decays to Note
---------- ---- --------- ----
b T [M][N] T (*)[N] Evaluates to address of b[0]
&b T (*)[M][N] n/a Evaluates to address of b
b[i] T [N] T * Evaluates to address of b[i][0]
&b[i] T (*)[N] n/a Evaluates to address of b[i]

Like above, all the *values* are the same (all of the expressions
evaluate to the same location), but the types are different.

3D arrays:

Declaration: T c[M][N][O];
Expression Type Decays To Note
---------- ---- --------- ----
c T [M][N][O] T (*)[N][O] Address of c[0]
&c T (*)[M][N][O] n/a Address of c
c[i] T [N][O] T (*)[O] Address of c[i][0]
&c[i] T (*)[N][O] n/a Address of c[i]
c[i][j] T [O] T * Address of c[i][j][0]
&c[i][j] T (*)[O] n/a Address of c[i][j]

Hopefully, by now the pattern is obvious for higher-dimensional
arrays.
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by Barry Schw » Sat, 20 Mar 2010 13:05:49

On Fri, 5 Mar 2010 13:12:48 -0600 (CST), MC < XXXX@XXXXX.COM >





If it got converted to a const pointer, then code of the form
char s[10];
char *p = s;
would require a diagnostic about loss of the const. >>char? >> >>>>>I think it is a pointer to a char array. >>> >>> Think again >>> >>>>>Or a pointer to a (const pointer to a char). >>> >>> Don't think about this again. >>> >>> >>> >>>>>Hence I am guessing the type of sp to be >>> >>>>>char * const * sp; >>> >>> No! pplying the & operator to an object of any type T produces a >>> value of type pointer to T. ince in this case T is array of 10 char, >>> the type of sp must be{*} pointer to array of 10 char, expressed in c >>> notation as >>> char (*sp)[10>> >>> >> {*} In addition to qualified types of this unqualified type, such a>> >> adding const, sp could also be a pointer to void. assume that bot>> >> these exceptions are outside the intent of your question>> >>> >>> >>> >> >But the compiler gives a warning >> >>> >> >I know I can define sp as char** and that will work, but I wanted t>> >>> >> No it won't. id you actually try it? ou should get exactly th>> >> same warning about incompatible pointer types>> >>> >> >know what exactly is the type of sp and how to define variables o>> >> >that type>> >>> >> While there are times when one wants to take the address of an arra>> >> using &, they are much rarer than the times one actually wants th>> >> address of the first element (yes it is the same address but it has >> >> different type). onsider the common case of passing an array of cha>> >> to strcpy>> >>> >> ->> >> Remove del for emai>> >> ->> >> comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you mus>> >> have an appropriate newsgroups line in your header for your mail to be seen>> >> or the newsgroup name in square brackets in the subject line. orry..

--
Remove del for email
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.
 
 
 

What type is address of an array

Post by Barry Schw » Sat, 20 Mar 2010 13:06:39

On Fri, 5 Mar 2010 11:55:26 -0600 (CST), XXXX@XXXXX.COM




The type of s is array of char. It is not a pointer though when the
array name is used in an expression it is frequently, but not always,
converted to a pointer expression.

Consider an array of 10 char
char s[10];
and a pointer to an array of 10 char
char (*p)[10];
the sizeof s must be exactly 10 while sizeof p is most likely either 4
or 8 depending on your system.

--
Remove del for email
--
comp.lang.c.moderated - moderation address: XXXX@XXXXX.COM -- you must
have an appropriate newsgroups line in your header for your mail to be seen,
or the newsgroup name in square brackets in the subject line. Sorry.