There is a warning/(error? I remember it is an error for line 10 on
some compilers before. At least on g++, it is an error.) for line 10. I
first read a similar example from `Expert C Programming -- Deep
Secrets, Perter van der Linden'. But I wonder why line 9 is ok but line
10 is illegal?
Is what Keith Thompson said in another post also helpful to understand
this question:
"... The implicit conversion rule applies only to void*, not to void**.
In this context, a void* is just another object type, and a void** is
just another pointer-to-object type. There's no implicit conversion
from one pointer-to-object type to another pointer-to-object type. ..."
void foo1(const char *p) {}
void foo2(const char **p) {}
int main(void)
{
char *p1;
char **p2;
foo1(p1); /* line 9: consistent type conversion */
foo2(p2); /* line 10: inconsistent type conversion */
}
The gcc tells:
$ gcc -W -Wall -std=c99 -pedantic test.c
....
test.c: In function `main':
test.c:10: warning: passing arg 1 of `foo2' from incompatible pointer
type 1 3250
lovecreatesbeauty wrote: There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming -- Deep Secrets, Perter van der Linden'. But I wonder why line 9 is ok but line 10 is illegal?
char pointer and const char pointer are both pointers to the same object
type, char.
A const char pointer and char pointer are different object types, so
pointers to them are pointers to different object types.
Is what Keith Thompson said in another post also helpful to understand this question:
"... The implicit conversion rule applies only to void*, not to void**. In this context, a void* is just another object type, and a void** is just another pointer-to-object type. There's no implicit conversion from one pointer-to-object type to another pointer-to-object type. ..."
Yes, Keith is pointing out that a pointer type is an object type and
there isn't any implicit conversion between pointers to different types.
void foo1(const char *p) {} void foo2(const char **p) {}
int main(void) { char *p1; char **p2;
foo1(p1); /* line 9: consistent type conversion */ foo2(p2); /* line 10: inconsistent type conversion */ }
The gcc tells: $ gcc -W -Wall -std=c99 -pedantic test.c .... test.c: In function `main': test.c:10: warning: passing arg 1 of `foo2' from incompatible pointer type
Mine says
"/tmp/x.c", line 10: warning: argument #1 is incompatible with prototype:
prototype: pointer to pointer to const char : "/tmp/x.c", line 2
argument : pointer to pointer to char
Which a a bit clearer.
--
Ian Collins. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Steven Reddie |
last post by:
I have a large char which I'd like to copy a portion of into a
vector<char>. I can't seem to locate a simple method for doing so. I
assume involving iterators will not result in memcpy not being...
|
by: Rakesh Sinha |
last post by:
I am having this code here.
static const float PI = 3.14159;
static const float INC = 0.4f * PI;
When I compile my program, I get the following error,
error: `MyClass::PI' cannot appear...
|
by: Tim Clacy |
last post by:
Please illuminate; what operator of class 'Event' will get matched for these
two cases :
Event ev1;
Event ev2;
// Case 1
//
if (ev1)
;
|
by: Charlie |
last post by:
Dear all,
I am stuck on this problem, couldn't find an answer on any C++ book in
the bookshop.
Following is a cut of my program, and its error message:...
|
by: p|OtrEk |
last post by:
What is practic difference between this two declarations?
If i want call my func with func("blah") i could write:
1) func(std::string const& arg1)
2) func(const std::string& arg1)
Whats better to...
|
by: S S |
last post by:
Hi Everyone
I have
const char *p = "Hello";
So, here memory is not allocated by C++ compiler for p and hence I
cannot access p to modify the contents to "Kello"
p = 'K'; // error at runtime
|
by: Angus |
last post by:
Hello
I have some classes defined in shapes.h and use them like this:
const IShape* rectangle = new Rectangle(10.0, 20.0); // width, height
cout << rectangle->calculateArea() << endl;
...
|
by: arnuld |
last post by:
i am trying to understand arrays and char. Stroustrup says, this a
string literal:
"this is a string literal"
he says it is of type /const char/. he also says, because of keeping...
|
by: Bill Davy |
last post by:
I want to be able to write (const char*)v where v is an item of type
Class::ToolTypeT where ToolTypeT is an enumeration and I've tried
everything that looks sensible. There's an ugly solution, but...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| | |