Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

Your good nature will bring unbounded happiness.


devel / comp.std.c / Re: why can change element of a const typed struct ?

SubjectAuthor
* why can change element of a const typed struct ?Denis Dos Santos Silva
+* why can change element of a const typed struct ?David Brown
|`* why can change element of a const typed struct ?Ben Bacarisse
| `- why can change element of a const typed struct ?David Brown
`- why can change element of a const typed struct ?Ben Bacarisse

1
why can change element of a const typed struct ?

<6d826a46-8852-4aa3-9e7d-23fac761e840n@googlegroups.com>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=1157&group=comp.std.c#1157

  copy link   Newsgroups: comp.std.c
X-Received: by 2002:a05:620a:406:b0:76d:8643:58b5 with SMTP id 6-20020a05620a040600b0076d864358b5mr226046qkp.13.1695052152803;
Mon, 18 Sep 2023 08:49:12 -0700 (PDT)
X-Received: by 2002:a05:6830:160f:b0:6b9:6ef7:72f3 with SMTP id
g15-20020a056830160f00b006b96ef772f3mr2881270otr.0.1695052152533; Mon, 18 Sep
2023 08:49:12 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.std.c
Date: Mon, 18 Sep 2023 08:49:12 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=190.111.150.210; posting-account=OCVt-woAAACOBpgUhUPvtozeKV-Qe0GA
NNTP-Posting-Host: 190.111.150.210
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6d826a46-8852-4aa3-9e7d-23fac761e840n@googlegroups.com>
Subject: why can change element of a const typed struct ?
From: denis@roo.com.br (Denis Dos Santos Silva)
Injection-Date: Mon, 18 Sep 2023 15:49:12 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1860
 by: Denis Dos Santos Sil - Mon, 18 Sep 2023 15:49 UTC

hi all!
why this works? =)

/// image.c
/// ...
typedef struct {
int w;
int h;
unsigned char channels;
unsigned char *data;
int err;
} image_t;

int image_copy(const image_t* source, const image_t* target, int xoffset, int yoffset) {
register int sindex;
register int tindex = 0;
register int y1, x1;

if (!source || !target)
return -1;

for (int y=0; y<target->h; y++) {
for (int x=0; x<target->w; x++) {
#if 1
y1 = y+yoffset;
x1 = x+xoffset;

sindex = (y1 * source->w + x1) * 3;
if (sindex > (source->w * source->h * 3))
continue;

// change value of element of const struct

source->data[sindex+0] = target->data[tindex++];
source->data[sindex+1] = target->data[tindex++];
source->data[sindex+2] = target->data[tindex++];
#endif
}
}

return 0;
} /// <eof>

Re: why can change element of a const typed struct ?

<uea4ti$1stu4$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=1158&group=comp.std.c#1158

  copy link   Newsgroups: comp.std.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.std.c
Subject: Re: why can change element of a const typed struct ?
Date: Mon, 18 Sep 2023 20:29:06 +0200
Organization: A noiseless patient Spider
Lines: 59
Message-ID: <uea4ti$1stu4$1@dont-email.me>
References: <6d826a46-8852-4aa3-9e7d-23fac761e840n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 18 Sep 2023 18:29:06 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="5506e3fbe207c7c43254297c6c7a33f1";
logging-data="1996740"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/qHOZhmkidAfstwF67IO+wnkItcWTDV04="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.13.0
Cancel-Lock: sha1:wJRuWrQwJm8yi/hYAKXFicBLdIA=
In-Reply-To: <6d826a46-8852-4aa3-9e7d-23fac761e840n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Mon, 18 Sep 2023 18:29 UTC

On 18/09/2023 17:49, Denis Dos Santos Silva wrote:
> hi all!
> why this works? =)
>

Your image_t is const, but it has a non-const pointer "data" - there is
no restriction to accessing the elements pointed to by source->data.

So your function can't change "source->data", but it /can/ change
"source->data[sindex]".

"const" does not pass through layers of pointers, it only applies to the
first pointed-at layer.

Does that help?

>
>
> /// image.c
> /// ...
> typedef struct {
> int w;
> int h;
> unsigned char channels;
> unsigned char *data;
> int err;
> } image_t;
>
> int image_copy(const image_t* source, const image_t* target, int xoffset, int yoffset) {
> register int sindex;
> register int tindex = 0;
> register int y1, x1;
>
> if (!source || !target)
> return -1;
>
> for (int y=0; y<target->h; y++) {
> for (int x=0; x<target->w; x++) {
> #if 1
> y1 = y+yoffset;
> x1 = x+xoffset;
>
> sindex = (y1 * source->w + x1) * 3;
> if (sindex > (source->w * source->h * 3))
> continue;
>
> // change value of element of const struct
>
> source->data[sindex+0] = target->data[tindex++];
> source->data[sindex+1] = target->data[tindex++];
> source->data[sindex+2] = target->data[tindex++];
> #endif
> }
> }
>
> return 0;
> }
> /// <eof>

Re: why can change element of a const typed struct ?

<87cyyfjnvl.fsf@bsb.me.uk>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=1159&group=comp.std.c#1159

  copy link   Newsgroups: comp.std.c
Followup: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.usenet@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.std.c
Subject: Re: why can change element of a const typed struct ?
Followup-To: comp.lang.c
Date: Mon, 18 Sep 2023 20:13:34 +0100
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <87cyyfjnvl.fsf@bsb.me.uk>
References: <6d826a46-8852-4aa3-9e7d-23fac761e840n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="4a1f049c3c0d7d3aa42e2e5e10f8a54b";
logging-data="2015877"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+izFAe+T/Vr/pZsUgaW4o64Trm5LnOKWU="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:o9yB3+3vC+l5F64/QjK0OJd81Fk=
sha1:3dwtsl6Jxh8PAVEtJqQqMMMFArQ=
X-BSB-Auth: 1.4fa0ad7dc205b6e86e6e.20230918201334BST.87cyyfjnvl.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 18 Sep 2023 19:13 UTC

Denis Dos Santos Silva <denis@roo.com.br> writes:

> hi all!

This would probably be better sent to comp.lang.c so I am setting the
followup-to header accordingly. This group is mainly about the ISO C
standard, though it's possible you are asking why the standard permits
this.

> why this works? =)
(from subject) why can change element of a const typed struct ?

Short answer: you are not changing any member of the struct. The
constness of the target of the pointer does transfer to other pointers
in the struct. data is const, but data[x] is not.

A detail... the struct is not const (well, it might be, but we can't
tell from this code). The struct is being accessed via pointer whose
target type is const. Now that would indeed require a diagnostic if the
code tried to change any of the members, but it does not.

> /// image.c
> /// ...
> typedef struct {
> int w;
> int h;
> unsigned char channels;
> unsigned char *data;
> int err;
> } image_t;
>
> int image_copy(const image_t* source, const image_t* target, int
> xoffset, int yoffset) {
....
> source->data[sindex+0] = target->data[tindex++];
> source->data[sindex+1] = target->data[tindex++];
> source->data[sindex+2] = target->data[tindex++];
....
> }
> /// <eof>

--
Ben.

Re: why can change element of a const typed struct ?

<871qevjls3.fsf@bsb.me.uk>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=1160&group=comp.std.c#1160

  copy link   Newsgroups: comp.std.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: ben.usenet@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.std.c
Subject: Re: why can change element of a const typed struct ?
Date: Mon, 18 Sep 2023 20:58:52 +0100
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <871qevjls3.fsf@bsb.me.uk>
References: <6d826a46-8852-4aa3-9e7d-23fac761e840n@googlegroups.com>
<uea4ti$1stu4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="4a1f049c3c0d7d3aa42e2e5e10f8a54b";
logging-data="2032392"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+chBaqD+BAY1H5qe8OudkLuaeFJB69c64="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux)
Cancel-Lock: sha1:jf8kb83TaKKBKks/tNm5YY17iDM=
sha1:MRr6vyNir0j0x0jNDnQIBg3nFng=
X-BSB-Auth: 1.1b1cfb700a7672da24e7.20230918205852BST.871qevjls3.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 18 Sep 2023 19:58 UTC

David Brown <david.brown@hesbynett.no> writes:

> On 18/09/2023 17:49, Denis Dos Santos Silva wrote:
>> hi all!
>> why this works? =)
>
> Your image_t is const, but it has a non-const pointer "data" - there is no
> restriction to accessing the elements pointed to by source->data.

I feel I must quibble because it can matter to someone learning C.

When accessed via 'const image_t *source', data /is/ (treated as) const.
data is a pointer, and the lvalue expression source->data is const
qualified. To call it a "non-const pointer" is using a common
shorthand, but one I've found is very confusing to beginners.

We casually talk about "const pointers" and "non-const pointers" because
we all know what we mean, but people learning C can get confused by what
is and is not const-qualified. It's a handy shorthand because an actual
'const pointer' is not seen so often:

char *const endp = start + strlen(start);

But we often see this

const char *end = start + strlen(start);

described as a const pointer even though changing the pointer is
perfectly valid:

end -= 1; // permitted because end is pointer that is not const

> So your function can't change "source->data",

Right, because data is treated as a const pointer. Calling it a
non-const pointer is potentially confusing. I know what you meant, but
is it clear to everyone?

> but it /can/ change "source->data[sindex]".
>
> "const" does not pass through layers of pointers, it only applies to the
> first pointed-at layer.

Your remark suggests that there is something special about one level of
indirection, but there isn't.

--
Ben.

Re: why can change element of a const typed struct ?

<uecduj$2dcj7$1@dont-email.me>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=1161&group=comp.std.c#1161

  copy link   Newsgroups: comp.std.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.brown@hesbynett.no (David Brown)
Newsgroups: comp.std.c
Subject: Re: why can change element of a const typed struct ?
Date: Tue, 19 Sep 2023 17:15:31 +0200
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <uecduj$2dcj7$1@dont-email.me>
References: <6d826a46-8852-4aa3-9e7d-23fac761e840n@googlegroups.com>
<uea4ti$1stu4$1@dont-email.me> <871qevjls3.fsf@bsb.me.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Tue, 19 Sep 2023 15:15:31 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="21b889700009786c696fe134d8bff631";
logging-data="2536039"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18muzlSQ4h6RxSKPPau+ctV8dpbUPyuaoI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:9c9lUzP3DaoxJ5H8wrySNwp7w5g=
Content-Language: en-GB
In-Reply-To: <871qevjls3.fsf@bsb.me.uk>
 by: David Brown - Tue, 19 Sep 2023 15:15 UTC

On 18/09/2023 21:58, Ben Bacarisse wrote:
> David Brown <david.brown@hesbynett.no> writes:
>
>> On 18/09/2023 17:49, Denis Dos Santos Silva wrote:
>>> hi all!
>>> why this works? =)
>>
>> Your image_t is const, but it has a non-const pointer "data" - there is no
>> restriction to accessing the elements pointed to by source->data.
>
> I feel I must quibble because it can matter to someone learning C.
>
> When accessed via 'const image_t *source', data /is/ (treated as) const.
> data is a pointer, and the lvalue expression source->data is const
> qualified. To call it a "non-const pointer" is using a common
> shorthand, but one I've found is very confusing to beginners.
>
> We casually talk about "const pointers" and "non-const pointers" because
> we all know what we mean, but people learning C can get confused by what
> is and is not const-qualified. It's a handy shorthand because an actual
> 'const pointer' is not seen so often:
>

Those are very good points, and I am glad you gave a better and more
accurate explanation than I did.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor