471,319 Members | 1,602 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

const c structure question

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
7 1379

"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
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
...
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
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
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
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
[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.

Similar topics

12 posts views Thread by Christof Krueger | last post: by
4 posts views Thread by Mahesh Tomar | last post: by
2 posts views Thread by Pavel | last post: by
11 posts views Thread by x-pander | last post: by
5 posts views Thread by Bill Pursell | last post: by
5 posts views Thread by Bit byte | last post: by
15 posts views Thread by Jiří Paleček | last post: by
39 posts views Thread by Leonardo Korndorfer | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.