const c structure question

G

Guest

Hi all,

I have this struct typedef:

typedef struct
{
int * test;
}testst;

and have this declaration:

const testst abc;

What is constant? The pointer (like in: int * const test) or the integer
(like in const int * test) or both?

Thanx,

Milux
 
M

Mike Wahler

nospam said:
Hi all,

I have this struct typedef:

typedef struct
{
int * test;
}testst;

and have this declaration:

const testst abc;

What is constant?

The object named 'abc'.
The pointer (like in: int * const test) or the integer (like in const int
* test) or both?

The entire object is const. Which means that each
individual member of the struct is const. Your struct
contains only one member, a pointer (type 'int*'), there
is no type 'int' member. So it's not a question of 'one
of or both', since that presumes two members. The pointer
'test' is const. What it points to (if anything) is not
const. A pointer to a const int is declared:

const int *p;

or

int const *p;


What specifically are you trying to do? Write
code for a particular task, or understand something
written by someone else?

-Mike
 
A

Alan Curry

Hi all,

I have this struct typedef:

typedef struct
{
int * test;
}testst;

For the purposes of exploring constness, the struct is unnecessary
obfuscation. The question would apply just as well to

typedef int *testp;
and have this declaration:

const testst abc;

const testp abcde;
What is constant? The pointer (like in: int * const test) or the integer
(like in const int * test) or both?

abcde is a const pointer to non-const int.
abc.test is also a const pointer to non-const int.
 
M

Milux

...
The object named 'abc'.
...
The entire object is const. Which means that each
individual member of the struct is const. Your struct
contains only one member, a pointer (type 'int*'), there
is no type 'int' member. So it's not a question of 'one
of or both', since that presumes two members. The pointer
'test' is const. What it points to (if anything) is not
const. A pointer to a const int is declared:

const int *p;

or

int const *p;


What specifically are you trying to do? Write
code for a particular task, or understand something
written by someone else?
I was exploring this because the structure contains variables declared
volatile. Thus
volatile int * const test;
But because the programm was not working correctly, I thought it had to do
with the object declared const. But I understand you explanation so this
part was correct, thanx.
 
B

Bart van Ingen Schenau

[restored attributions]
I was exploring this because the structure contains variables declared
volatile. Thus
volatile int * const test;
But because the programm was not working correctly, I thought it had
to do with the object declared const. But I understand you explanation
so this part was correct, thanx.

While exploring this, you should be aware that 'const' is actually very
badly named.
The effect of const-qualifying something is only to mark it as
read-only. It does not make it constant/unchangeable.

For example, on machines that allow direct access to (memory mapped)
hardware, you could have something like this
const volatile int * const current_time = (const volatile int*)0x1234;
to gain access to a hardware timer.
Through the variable current_time, you are allowed to read the current
value of the timer, but you can not modify it from within the program.
Obviously, the value you read will differ over time.
Bart v Ingen Schenau
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top