Borked Pseudo Mailed
Pointer to pointer
James Kuyper said:
> printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
The "%p" format specifier expects a void* argument. This is one of the
few places where a cast is routinely needed and perfectly correct:printf("*a=%p *p=%p %c %s\n", (void*)*a, (void*)*p, **p, *p);
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
Ben Bacarisse said:
> printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);Technical matter: %p expects arguments of type void * and the
conversion is not done automatically so you need to write:printf("*a=%p *p=%p %c %s\n", (void *)*a, (void *)*p, **p, *p);
I'm not sure James and Ben have this right, but I'd say it's more
probable I'm wrong here. Still, we'll see. CLC psychology dictates
that if I'm wrong, a dozen people with a grudge will be quick to
correct me.
6.2.5 (27) says the following:
"A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type."
And then a footnote for that says:
"The same representation and alignment requirements are meant to
imply interchangeabil ity as arguments to functions ..."
The OP is passing a (char *) to %p. Therefore, he doesn't need to
cast the printf() arguments to (void *).
This is the Unix execl() case in reverse. The function execl(),
for those terminally in ANSI/ISO C land, uses a null character
pointer - i.e., (char *)0 or (char *)NULL - to indicate the end
of its variable-length argument list.
execl("/path/file", "arg", "arg", "arg", NULL);
works if NULL is defined as ((void *) 0), whereas it's not
guaranteed to work if NULL is defined as 0.
Yours,
Han from China
Nov 21 '08 #1
Subscribe Reply
3 1223
Richard Tobin
In article <92************ *************** ***@pseudo.bork ed.net>,
Borked Pseudo Mailed <no****@pseudo. borked.netwrote :
>> printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
>I'm not sure James and Ben have this right, but I'd say it's more
probable I'm wrong here. Still, we'll see. CLC psychology dictates
that if I'm wrong, a dozen people with a grudge will be quick to
correct me.
Okey dokey.
"A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type."
The supposed problem here is that even though they have the same
representation, they may be passed to variadic functions in a
different way. One can imagine this really happening for, say,
ints and doubles, but that it should happen for char * and void *
is implausible.
>And then a footnote for that says:"The same representation and alignment requirements are meant to
imply interchangeabil ity as arguments to functions ..."
Footnotes are not normative, but this one strongly suggests that the
committee meant it to work.
-- Richard
--
Please remember to mention me / in tapes you leave behind.
Nov 21 '08 #2
Ben Bacarisse
Borked Pseudo Mailed <no****@pseudo. borked.netwrite s:
Pointer to pointerJames Kuyper said:
>> printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
The "%p" format specifier expects a void* argument. This is one of the
few places where a cast is routinely needed and perfectly correct:printf("*a=% p *p=%p %c %s\n", (void*)*a, (void*)*p, **p, *p);
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
Ben Bacarisse said:>> printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);Technical matter: %p expects arguments of type void * and the
conversion is not done automatically so you need to write:printf("*a=%p *p=%p %c %s\n", (void *)*a, (void *)*p, **p, *p);
I'm not sure James and Ben have this right, but I'd say it's more
probable I'm wrong here.
In the end, my view is that there is no way to escape the effect of
the "shall be a pointer to void" in the description of %p. See
section 4 (Conformance) paragraph 2 -- violating a shall (that is not
a constraint) is undefined behaviour.
Still, we'll see. CLC psychology dictates
that if I'm wrong, a dozen people with a grudge will be quick to
correct me.6.2.5 (27) says the following:
"A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type."And then a footnote for that says:
"The same representation and alignment requirements are meant to
imply interchangeabil ity as arguments to functions ..."The OP is passing a (char *) to %p. Therefore, he doesn't need to
cast the printf() arguments to (void *).
Despite the same representation and alignment, printf (and friends)
may do something non-C-like and specifically permitted (by, say, the
hardware) only for void *s since the standard assures the
implementation that the argument will be a void * and nothing else.
I find it hard to image that it will fail (for a char *) but I also
find it much simpler to cast to void * as specified by the
specification of printf. If I were forced into an alternate, more
expensive, algorithm by a corner case in the standard I might be
tempted to document the choice and wing it, but that is not the case
here.
--
Ben.
Nov 21 '08 #3
jameskuyper
Borked Pseudo Mailed wrote:
Pointer to pointerJames Kuyper said:
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);The "%p" format specifier expects a void* argument. This is one of the
few places where a cast is routinely needed and perfectly correct:printf("*a=%p *p=%p %c %s\n", (void*)*a, (void*)*p, **p, *p);
printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);
Ben Bacarisse said:printf("*a=%p *p=%p %c %s\n", *a, *p, **p, *p);Technical matter: %p expects arguments of type void * and the
conversion is not done automatically so you need to write:printf("*a=%p *p=%p %c %s\n", (void *)*a, (void *)*p, **p, *p);
I'm not sure James and Ben have this right, but I'd say it's more
probable I'm wrong here. Still, we'll see. CLC psychology dictates
that if I'm wrong, a dozen people with a grudge will be quick to
correct me.6.2.5 (27) says the following:
"A pointer to void shall have the same representation and alignment
requirements as a pointer to a character type."And then a footnote for that says:
"The same representation and alignment requirements are meant to
imply interchangeabil ity as arguments to functions ..."
The tricky point is that while "it is meant to imply
interchangeabil ty", it doesn't actually imply it. It's quite feasible
(easy, even) to invent a function calling convention where having the
same representation and alignment is not sufficient to ensure
interchangeabil ity. To take the most direct approach, consider a
function calling convention where each argument is explicitly tagged
according to type. Less directly, consider an API that passes void*
pointers through a different method than char* pointers.
The key point is that while the footnote expresses the intent, the
actual words of the standard fail to mandate that the intent be
achieved. I'd be much happier if the standard said explicitly and
directly, in the normative text (footnotes are not normative), that
these type were interchangeable for the specified purposes.
Nov 21 '08 #4
This thread has been closed and replies have been disabled. Please start a new discussion.
Similar topics
1 1959 (part 11) Han from China answers your C questions by: Nomen Nescio |last post by: A new learner's question Flash Gordon wrote: To the OP, please be aware that Flash Gordon has a history of either deliberately misleading and "miss-informing" posters to this newsgroup or providing facile answers with which to amuse himself. In this case, both. Facile because Flash Gordon did not tell you substantially anything more than your compiler had told you. Misleading C / C++ |
1479 (part 12) Han from China answers your C questions by: Nomen Nescio |last post by: what is the type for this call? Neo said: Neo, buddy, I've decided to give you a little summary for reference so that you don't have to sift through excess verbiage, cryptic meanderings, and irrelevant matters of naming to make sense of this thing. Please forgive any typos on my part. C / C++ |
1299 (part 14) Han from China answers your C questions by: Nomen Nescio |last post by: No format string passed to variable argument list function Adam said: Then Adam laters says: Then Adam later says: C / C++ |
2 1504 Re: (part 15) Han from China answers your C questions by: Chris Dollin |last post by: Joachim Schmitz wrote: I think that remark is an ill-considered and unhelpful generalisation. -- "We dance, and the worlds melt away." - Curved Air, /Metamorphosis/ Hewlett-Packard Limited registered no: registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England C / C++ |
2 1539 (part 23) Han from China answers your C questions by: Borked Pseudo Mailed |last post by: float and integers c.lang.myself@gmail.com said I will be making assumptions here about your system that will infuriate the pedants, but I don't care. Given your X-HTTP-UserAgent string, it is highly probable that these assumptions are valid. Whether you have included stdio.h or not, the float argument to printf() is promoted to a double, since even with a prototype C / C++ |
1 1485 (part 22) Han from China answers your C questions by: George Orwell |last post by: its urgent.pls help me. mona said: mona said: Hey again, mona. Please check the thread "(part 21) Han from China answers your C questions", in which I gave you a program with C / C++ |
1371 (part 27) Han from China answers your C questions by: George Orwell |last post by: fread breaks file descriptors opened in "w" mode. ... Yes, once you break out of ANSI/ISO C, all things are possible. You have posted an on-topic question to comp.lang.c. See version 2.0 of my topicality FAQ if anyone should tell you otherwise. Search: "Han from China's comp.lang.c topicality FAQ (version 2.0)" C / C++ |
16 1812 (part 30) Han from China answers your C questions by: Borked Pseudo Mailed |last post by: Error handling in C Nick Keighley said: ... ... To the OP, please be aware that Nick Keighley is a known troll and is deliberately providing you with unsound advice. You asked for error handling, not error generation (or much worse). C / C++ |
2 2205 (part 29) Han from China answers your C questions by: Nomen Nescio |last post by: What is this? Eric said: That looks like either someone's poor coding or someone's poor pasting for an implementation of offsetof() from stddef.h. The (char *) leads me to think that there's plenty of missing context, so post the surrounding code, and we'll be happy to jerk ourselves off analyzing the code for you and telling you why it won't work on the control system for your Tomahawk missile. C / C++ |
1 1597 (part 32) Han from China answers your C questions by: George Orwell |last post by: File Seeking / Overwriting bytes Martien Verbruggen said: And this is, of course, also not right. fseek() does support SEEK_END on text streams. If you want to know why, please start a thread on comp.lang.c. Also, you could read "(part 18) Han from China answers your C questions". C / C++ |
9420 Changing the language in Windows 10 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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,... Windows Server |
10035
Maximizing Business Potential: The Nexus of Website Design and Digital Marketing 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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is... Online Marketing |
1 9982 The easy way to turn off automatic updates for Windows 10/11 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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,... Windows Server |
9851
Discussion: How does Zigbee compare with other wireless protocols in smart home applications? 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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some... General |
8862
AI Job Threat for Devs 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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea... Career Advice |
1 7398
Access Europe - Using VBA to create a class based on a table - Wed 1 May 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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will... Microsoft Access / VBA |
6662 Couldnt get equations in html when convert word .docx file to html file in C#. 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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();... C# / C Sharp |
5293 Trying to create a lan-to-lan vpn between two differents networks 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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls... Networking - Hardware / Configuration |
2 3554
How to add payments to a PHP MySQL app. by: muto222 |last post by: How can i add a mobile payment intergratation into php mysql website. PHP |