Sujet : Re: Arrays
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 29. Sep 2024, 17:11:08
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240929085744.827@kylheku.com>
References : 1
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-09-29, Stefan Ram <
ram@zedat.fu-berlin.de> wrote:
Alright, so I've got a triple whammy of questions coming at me.
It's about this program I've been tinkering with.
>
#include <stdio.h>
int main()
{ char const abc[] = "abcdefhijklmnopqrstuvwxyz";
char const * const p = abc + 3;
char const( * const def )[] =( char const( * const )[] )p;
^^^^^^^^^^^^^^^^^^^^^^^^^
]
This is a const pointer to an array (of unknown size) to const char.
printf( "%c\n",( *def )[ -3 ]); }
>
1st
>
First off, I'm getting this warning that's throwing me for a loop:
>
main.c: In function 'main':
main.c:7:32: warning: cast discards 'const' qualifier from pointer target type [-Wcast-qual]
7 | char const( * const def )[] =( char const( * const )[] )p;
The problem is that def is a pointer to an array of const char.
The const char elements are const qualified, but the array type is not!
That's why a qualifier is being stripped in the cast.
So I'm wondering: How can I dodge this discarding the constness
warning?
I would say, don't work with pointers to arrays.
There is no way to declare a const array in declarator syntax.
You must create a typedef name N for the array type and then
refer to it as const N.
2nd
>
Then I'm hit with another warning:
>
main.c:7:3: warning: dereferencing type-punned pointer might break strict-aliasing rules [-Wstrict-aliasing]
7 | char const( * const def )[] =( char const( * const )[] )p;
| ^~~~
>
What's this warning trying to tell me?
That you're banging your head against array-pointer-const corner cases
in the C language.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca