473,326 Members | 2,125 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,326 software developers and data experts.

(part 38) Han from China answers your C questions

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 interchangeability 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
3 1209
In article <92******************************@pseudo.borked.ne t>,
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 interchangeability 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
Borked Pseudo Mailed <no****@pseudo.borked.netwrites:
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.
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 interchangeability 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
Borked Pseudo Mailed wrote:
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 interchangeability as arguments to functions ..."
The tricky point is that while "it is meant to imply
interchangeabilty", 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
interchangeability. 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
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...
0
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,...
0
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:
2
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/ ...
2
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...
1
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
0
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...
16
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...
2
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...
1
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.