0

I'm trying to make an alias (firstElementNum) to an array element for the sake of abstracting complexity away from the programmer. Here's my code:

typedef struct {
    int structNum;
} myStruct;

myStruct myArray[10];

int *const firstElementNum = myArray[0].structNum;

When I tried this, I got initializer element is not a constant. I guess that means the right hand side needs to be a defined constant. So how am I supposed to assign this pointer to my target element?

3 Answers 3

3

It sounds like from your questions that you are unclear on the fundamental rules for pointers. Many people who post questions in the C tag are unclear on this. Basically:

  • A storage location can hold a value of a particular type.
  • If you apply the & operator to a storage location you get a pointer to the associated type.
  • A pointer is a value.
  • If you apply the * operator to a pointer you get a storage location.

Technically a storage location is called an "lvalue" but that seems needlessly jargonish for the beginner.

So let's take a look:

int *const firstElementNum = myArray[0].structNum;

That's not right. structNum is a storage location that contains a value of type int. But to assign to firstElementNum we want an expression of type "pointer to int".

Try again:

int *const firstElementNum = &myArray[0].structNum;

That's better. structNum is a storage location of type int. The & operator gives you a pointer to int. That's a value that can be stored in storage location firstElementNum.

firstElementNum = 5;

This is doubly wrong; first, you're trying to write to a const storage location, and second, this is trying to assign a value of type int to a storage location of type "pointer to int".

*firstElementNum = 5;

This is right. firstElementNum is a storage location that contains a pointer to int. Applying * to that value makes a storage location of type int, which is what you need.

Sign up to request clarification or add additional context in comments.

Comments

2
int *const firstElementNum = &myArray[0].structNum;

Note the "address of" operator, &, since you want firstElementNum to point to the address of myArray[0].structNum.

2 Comments

Excellent. But to assign it, firstElementNum = 5 for example complains that I'm trying to write to a read-only variable - which signifies it's trying to overwrite the pointer. It stopped complaining if I did *firstElementNum, so do I have to dereference my alias every time I read to or write from it?
@CJxD yes, that's correct. A pointer is not strictly an alias.
0

To understand what int *const is: read it from backwards ie. constant pointer to an integer.

so you must assign it to a pointer, not an integer

int *const firstElementNum = &myArray[0].structNum;

Now, you can access the value using the deference operator: * firstElementNum.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.