473,569 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const void * const x

What is the point of the construct in the subject line? Why the use of
two const labels? Does that do something different than const void *x?
And what would void * const x indicate? Thanks in advance :)
--
Tobias DiPasquale

Nov 14 '05 #1
17 2638
codeslinger wrote:
What is the point of the construct in the subject line? Why the use of
two const labels? Does that do something different than const void *x?
And what would void * const x indicate? Thanks in advance :)


It means that neither the pointer nor what it points to may be changed.

HTH,
--ag

--
Artie Gold -- Austin, Texas

"What they accuse you of -- is what they have planned."
Nov 14 '05 #2
Tobias DiPasquale wrote:
What is the point of the construct in the subject line?
Why the use of two const labels?
Both x and *x are constant.
Does that do something different than const void *x?
Yes.
And what would void * const x indicate?
It indicates that x is a constant.
cat main.c #include <stdio.h>
#include <stdlib.h>

int* ramp(size_t n) {
int* p = (int*)malloc(n* sizeof(int));
for (size_t j = 0; j < n; ++j)
p[j] = j;
return p;
}

int main(int argc, char* argv[]) {
const
size_t n = 32;
const
int* const p = ramp(n);
//p = NULL; // error! p is a constant
//p[0] = 13; // error! *p is a constant
for (size_t j = 0; j < n; ++j)
fprintf(stdout, "%2d = p[%2d]\n", p[j], j);
free((void*)p);
return 0;
}
gcc -Wall -std=c99 -pedantic -o main main.c
./main

0 = p[ 0]
1 = p[ 1]
Nov 14 '05 #3
Artie Gold <ar*******@aust in.rr.com> writes:
codeslinger wrote:
What is the point of the construct in the subject line? Why the use of
two const labels? Does that do something different than const void *x?
And what would void * const x indicate? Thanks in advance :)


It means that neither the pointer nor what it points to may be changed.


Right, but since it points to void, what it points to can't be changed
anyway.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
Keith Thompson wrote:
Artie Gold <ar*******@aust in.rr.com> writes:
codeslinger wrote:
What is the point of the construct in the subject line? Why the use of
two const labels? Does that do something different than const void *x?
And what would void * const x indicate? Thanks in advance :)


It means that neither the pointer nor what it points to may be changed.

Right, but since it points to void, what it points to can't be changed
anyway.

Point taken. :-)

--ag

--
Artie Gold -- Austin, Texas

"What they accuse you of -- is what they have planned."
Nov 14 '05 #5
Keith Thompson wrote:
Artie Gold writes:
codeslinger wrote:
What is the point of the construct in the subject line? Why the use of
two const labels? Does that do something different than const void *x?
And what would void * const x indicate? Thanks in advance :)


It means that neither the pointer nor what it points to may be changed.


Right, but since it points to void,
what it points to can't be changed anyway.


#include <string.h>

void *memcpy(void *dest, const void *src, size_t n);

Are you saying that memcpy cannot change what dest points to
just because dest is of type void*?
Nov 14 '05 #6
In 'comp.lang.c', Keith Thompson <ks***@mib.or g> wrote:
Artie Gold <ar*******@aust in.rr.com> writes:
codeslinger wrote:
> What is the point of the construct in the subject line? Why the use of
> two const labels? Does that do something different than const void *x?
> And what would void * const x indicate? Thanks in advance :)


It means that neither the pointer nor what it points to may be changed.


Right, but since it points to void, what it points to can't be changed
anyway.


Nonsense. A pointer can be aliased by a typed one. The point is that the
alias must be const too.

char s[] = "Hello"; /* R/W data */
void const *p = s; /* not dereferencable. */
char *pa = p; /* Diagnostic */
char const *pb = p; /* Correct. Read only acces */

For a more real approach, think in terms of parameters.

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #7
In <cc**********@n ntp1.jpl.nasa.g ov> "E. Robert Tisdale" <E.************ **@jpl.nasa.gov > writes:
Keith Thompson wrote:
Artie Gold writes:
codeslinge r wrote:

What is the point of the construct in the subject line? Why the use of
two const labels? Does that do something different than const void *x?
And what would void * const x indicate? Thanks in advance :)

It means that neither the pointer nor what it points to may be changed.


Right, but since it points to void,
what it points to can't be changed anyway.


#include <string.h>

void *memcpy(void *dest, const void *src, size_t n);

Are you saying that memcpy cannot change what dest points to
just because dest is of type void*?


Nope, he's *obviously* saying that dest itself cannot be used for this
purpose: it has to be converted to an object pointer type first.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
In <Xn************ *************** @212.27.42.70> Emmanuel Delahaye <em**********@n oos.fr> writes:
In 'comp.lang.c', Keith Thompson <ks***@mib.or g> wrote:
Artie Gold <ar*******@aust in.rr.com> writes:
codeslinger wrote:
> What is the point of the construct in the subject line? Why the use of
> two const labels? Does that do something different than const void *x?
> And what would void * const x indicate? Thanks in advance :)

It means that neither the pointer nor what it points to may be changed.
Right, but since it points to void, what it points to can't be changed
anyway.


Nonsense. A pointer can be aliased by a typed one.


But you're still not using the original pointer for this purpose. So,
where is the nonsense?!?
The point is that the alias must be const too.
If there is such a point, your example doesn't reflect it.
char s[] = "Hello"; /* R/W data */
void const *p = s; /* not dereferencable. */
char *pa = p; /* Diagnostic */
char const *pb = p; /* Correct. Read only acces */


I can't see any pointer aliasing another pointer in your example. Are
you sure you know what you're talking about? And what's incorrect with

char *pa = (char *)p;

? You can even use pa in write mode, because the pointed-to data is
modifiable. It didn't become read-only simply because you pointed a
pointer to const at it.

Far too often, using the type pointer to const means: "I promise not to
use this pointer to modify the data" rather than "this pointer points to
non-modifiable data". Just as in your example.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #9
In 'comp.lang.c', Da*****@cern.ch (Dan Pop) wrote:
In 'comp.lang.c', Keith Thompson <ks***@mib.or g> wrote:
Artie Gold <ar*******@aust in.rr.com> writes:
codeslinger wrote:
> What is the point of the construct in the subject line? Why the use
> of two const labels? Does that do something different than const
> void *x? And what would void * const x indicate? Thanks in advance
> :)

It means that neither the pointer nor what it points to may be
changed.

Right, but since it points to void, what it points to can't be changed
anyway.
Nonsense. A pointer can be aliased by a typed one.


But you're still not using the original pointer for this purpose. So,
where is the nonsense?!?


<quote>
"but since it points to void, what it points to can't be changed anyway."
</>

This is wrong. It's not because a pointer is void that the pointed data is
not accessible, at least indirectly.
The point is that the alias must be const too.


If there is such a point, your example doesn't reflect it.
char s[] = "Hello"; /* R/W data */
void const *p = s; /* not dereferencable. */
char *pa = p; /* Diagnostic */
char const *pb = p; /* Correct. Read only acces */


I can't see any pointer aliasing another pointer in your example. Are
you sure you know what you're talking about?


I came with the idea that 'aliasing' was the result of more than one pointer
pointing to the same location. If you have a better definition, I'd be glad
to ear it.

Hence, in my example, 'pa' and 'pb' are aliasing 'p'. My wording might be
incorrect; correctness welcome.
char *pa = (char *)p;
Evil!
? You can even use pa in write mode, because the pointed-to data is
modifiable. It didn't become read-only simply because you pointed a
pointer to const at it.

Far too often, using the type pointer to const means: "I promise not to
use this pointer to modify the data" rather than "this pointer points to
non-modifiable data". Just as in your example.


Hehe! Abusive typecast users will burn in hell ;-)

--
-ed- get my email here: http://marreduspam.com/ad672570
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2410
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk implicitly. The destructor of this class saves the object to the disk if it is dirty. The problem comes in the following scenario when a function returns an...
4
1969
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 some typedef structure and offcourse x is a pointer to it. Needless to say my intention for writing such functions in C was to protect the accident...
11
2498
by: modemer | last post by:
If I define the following codes: void f(const MyClass & in) {cout << "f(const)\n";} void f(MyClass in) {cout<<"f()\n";} MyClass myclass; f(myclass); Compiler complain that it can't find the best match. Anyone could give a detail explanation in theory? Which one is good?
13
2322
by: herrcho | last post by:
int intcmp(const void *a, const void *b) { return (*(int*)a - *(int*)b); } in the above , if i put just 'void' instead of 'const void' as a parameter, what's the difference ?
7
4349
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
14
2431
by: Enrico `Trippo' Porreca | last post by:
Given: typedef struct Node Node; struct Node { void *obj; Node *next; }; typedef struct Stack Stack; struct Stack {
6
10007
by: Geoffrey S. Knauth | last post by:
It's been a while since I programmed in C++, and the language sure has changed. Usually I can figure out why something no longer compiles, but this time I'm stumped. A friend has a problem he hoped I could solve, and I couldn't. Some code he's using, written in 1999, that compiled fine in 1999, no longer does in 2006 with g++ 4. This...
16
3145
by: hzmonte | last post by:
Correct me if I am wrong, declaring formal parameters of functions as const, if they should not be/is not changed, has 2 benefits; 1. It tells the program that calls this function that the parameter will not be changed - so don't worry. 2. It tells the implementor and the maintainer of this function that the parameter should not be changed...
4
6677
by: grizggg | last post by:
I have searched and not found an answer to this question. I ran upon the following statement in a *.cpp file in a member function: static const char * const pacz_HTMLContentTypeHeader = "Content-Type: text/html\r\n"; Why is the second const needed and what does it do? Thanks
0
1925
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*. I am also going to compare a grid map to a hex map, which is why I want to make a generic base graph class that gridmap and hexmap will inherit from....
0
7922
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5218
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1209
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.