473,372 Members | 1,073 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 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 1480

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: kaede | last post by:
Hi all, Consider the following code fragment: // some data structure class Data { ... } // Container for the data structure Class Container {
12
by: Christof Krueger | last post by:
Hello, I'm quite new to C++ so maybe there's something I miss. I write a simple board game. It has a board class. This class has a method that returns the count of pieces a player has on the...
4
by: Mahesh Tomar | last post by:
Dear Readers, I am porting my existing C code to C++. In my existing code there are numerous functions that has been defined with CONST qualifier. For eg. foo(const DATA_TYPE *x); DATA_TYPE is...
2
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
5
by: Bill Pursell | last post by:
Suppose I have a structure with many members, and I pass a pointer to that structure to a function. I'd like the prototype of the function to specify that it will only be changing certain members...
5
by: Bit byte | last post by:
I have the following methods: static void Foo::setBar(const Bar*) ; //store a copy of Bar static const Bar* Foo::getBar(void) const ; //return an UNMODIFIABLE ptr to our internal copy In...
15
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
39
by: Leonardo Korndorfer | last post by:
Hi, I'm litle confused by the const modifier, particularly when use const char* or char*. Some dude over here said it should be const char when you dont modify it content inside the function, I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.