473,473 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

evaluate NULL to a pointer which a CONST pointer points to

G
Hi~
you guys.
I come across the code below:
/////////////////////////////////////////////////////////
#include <stdio.h>
void foo(const char **pp)
{
*pp=NULL;
//it works too
//*pp="Hello world!";
}

int main()
{
const char *p="hello";
foo(&p);
printf("%s",p);
return 0;
}
//////////////////////////////////////////////////////////
It was compiled successed without no warn or error.

Why can *pp change , though " const char **pp " meant it couldn't ?

Thank you ! :-)

Jan 8 '07 #1
3 1234
G
Well,I got the answer.

Jan 8 '07 #2
G
Well,I have got the answer.

Jan 8 '07 #3
"G" <gp****@gmail.comwrote in message
news:11********************@v33g2000cwv.googlegrou ps.com...
Well,I got the answer.
Then post the answer, so others can benefit as well! :)

So for the sake of completeness: you are allowed to change the pointer
because the 'pointer to const char' that the pointer points to is itself not
const. This may be a bit difficult to understand at first, so a few typedefs
would clear things up:

----------
typedef int the_type_pointed_to;

void foo(the_type_pointed_to * ptr)
{
*ptr = 3; // allowed
}

void foo(the_type_pointed_to const * ptr)
{
*ptr = 4; // not allowed, the_type_pointed_to is const.
}
----------

This is of course very obvious. Now, change the_type_pointed_to to be a char
const * rather than an int. You'll see that both overloads of foo are still
different - one works on a pointer to nonconst 'char const *', while the
other works on a pointer to _const_ 'char const *'. If you substitute
the_type_pointed_to with the actual type in the second definition, it would
read:
void foo(char const * const * ptr)

So it doesn't matter whether **ptr is const or when you want to change *ptr,
it matters whether *ptr itself is const!

- Sylvester
Jan 8 '07 #4

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

Similar topics

10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
10
by: quantdev2004 | last post by:
Hi all, I have been deling with this kind of code: class Foo { public: void NonConstMethod() {} };
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
1
by: Tomás | last post by:
Some programmers treat arrays just like pointers (and some even think that they're exactly equivalent). I'm going to demonstrate the differences. Firstly, let's assume that we're working on a...
41
by: Dead Loop | last post by:
Hi all, I'm a beginner and my question is: Are there any differences between char *p = "Hello, world!"; and const char *p = "Hello, world!"; ?
11
by: Christopher Key | last post by:
Hello, Can anyone suggest why gcc (-W -Wall) complains, test.c:22: warning: passing arg 1 of `f' from incompatible pointer type when compiling the following code? Change the declation of f...
27
by: rocco.rossi | last post by:
I've been trying to write a function capable of checking if a pointer value is set to NULL, and if it isn't, of deallocating and setting it's value to NULL regardless of the pointer's type. I...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...
0
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...
0
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...
0
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...

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.