admin管理员组文章数量:1133713
I have an array of structures.
typedef struct { int a; int b; } s;
static const s info[2] = {
{.a=1, .b=2}, {.a=3, .b=4 }};
I also have a pointer to this kind of struct:
static s *thisInfo;
I want to set thisInfo
to point to one element of the array info
. This is on an embedded processor and I want s
to be const
so that it stays in the Flash memory.
thisInfo = &info[0];
But that gets the message
warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
I have an array of structures.
typedef struct { int a; int b; } s;
static const s info[2] = {
{.a=1, .b=2}, {.a=3, .b=4 }};
I also have a pointer to this kind of struct:
static s *thisInfo;
I want to set thisInfo
to point to one element of the array info
. This is on an embedded processor and I want s
to be const
so that it stays in the Flash memory.
thisInfo = &info[0];
But that gets the message
warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
Share
Improve this question
edited Jan 7 at 17:46
Chris
36.3k5 gold badges31 silver badges54 bronze badges
asked Jan 7 at 17:35
ParallelParallel
111 silver badge1 bronze badge
New contributor
Parallel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
11
|
Show 6 more comments
1 Answer
Reset to default 5Since the elements of info
are of type const s
, a pointer to such an element should be of type const s*
. So you should declare
static const s *thisInfo;
Note this does not mean that thisInfo
itself is const; you can still modify the pointer freely. But you can't modify the object that it points to.
thisInfo = &info[0]; // ok
thisInfo = &info[1]; // ok
thisInfo->a = 0; // compiler error: assignment of member 'a' in read-only object
本文标签: Getting address of one struct within an array in CStack Overflow
版权声明:本文标题:Getting address of one struct within an array in C - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736786138a1952874.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
info[0]
is constant, andthisInfo
is a pointer to a non-constant structure. – Some programmer dude Commented Jan 7 at 17:40const
correctness. Arrays,static
and your embedded target are irrelevant to that issue. BTW: Just search for the error message! – Ulrich Eckhardt Commented Jan 7 at 17:43