By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,404 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

const c structure question

P: n/a
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
Jun 5 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a

"nospam" <""miluxkloosterman\"@(nospam)home.nl"wrote in message
news:f4**********@netlx020.civ.utwente.nl...
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
Jun 5 '07 #2

P: n/a
In article <f4**********@netlx020.civ.utwente.nl>,
nospam <""miluxkloosterman\"@(nospam)home.nl"wrote:
>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.

--
Alan Curry
pa****@world.std.com
Jun 5 '07 #3

P: n/a
...
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.
-Mike


Jun 5 '07 #4

P: n/a
nospam wrote:
>
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?
Nothing. You failed to initialize abc. This is the only place you
can set its value. F'ups set.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com

Jun 6 '07 #5

P: n/a
nospam wrote:
>
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?
Nothing. You failed to initialize abc. This is the only place you
can set its value.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 6 '07 #6

P: n/a
nospam wrote:
>
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?
Nothing. You failed to initialize abc. This is the only place you
can set its value. F'ups set.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 6 '07 #7

P: n/a
[restored attributions]

Milux wrote:
Mike Wahler wrote:
>>
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.
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.
>
>-Mike

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/
Jun 7 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.