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
  • The error message might not be all that clear, but it's basically is saying that info[0] is constant, and thisInfo is a pointer to a non-constant structure. – Some programmer dude Commented Jan 7 at 17:40
  • This is a basic issue const 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
  • Not only are they irrelevant, assuming the location of the array seems incredibly brittle design -- "I want s to be const so that it stays in the Flash memory". By brittle I mean shoddy, not "you're right in this case, but not in general". – Blindy Commented Jan 7 at 17:48
  • @Blindy They're targeting a microcontroller, I think it's safe to assume it has options to ensure that. – Barmar Commented Jan 7 at 17:50
  • You can think that if you want, the fact of the matter is that nothing really stops anyone from getting a non-const pointer to that array (as he literally did in his question). At that point it's no longer about your assumptions, the compiler will absolutely not use read-only memory for it, and you as the writer of that array in the first place will have no idea. Brittle is an understatement. – Blindy Commented Jan 7 at 17:52
 |  Show 6 more comments

1 Answer 1

Reset to default 5

Since 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