Pointer Qs

K

Kamus of Kadizhar

I used to do lots and lots of C coding back in the pre-ANSI days. I just
picked up a project that has lots of pointers running around.

Now I'm seeing things like

char utf8char[6];
....
xxx = letter((char *)&utf8char);

where

void letter( char *somechar) {
....
}

Back in the "old" days we would have simply said:

char utf8char[6];
....
xxx = letter(utf8char);

so what's the purpose of the (char *)& shenanigans?

I mean, it works, so it must be right, but I always thought that using an
array name without the subscript returns a pointer to the first element
anyway....

--Kamus
 
T

Thomas Matthews

Kamus said:
I used to do lots and lots of C coding back in the pre-ANSI days. I just
picked up a project that has lots of pointers running around.

Now I'm seeing things like

char utf8char[6];
...
xxx = letter((char *)&utf8char);

where

void letter( char *somechar) {
...
}

Back in the "old" days we would have simply said:

char utf8char[6];
...
xxx = letter(utf8char);

so what's the purpose of the (char *)& shenanigans?

I mean, it works, so it must be right, but I always thought that using an
array name without the subscript returns a pointer to the first element
anyway....

--Kamus

Looks like redundant casting.
An array decays into a pointer to the first element.
Search this newsgroup for "The Rule" by Chris Torek.

Your version is valid and alot simpler.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
A

Arthur J. O'Dwyer

[re-arranged a little]

I used to do lots and lots of C coding back in the pre-ANSI days. I just
picked up a project that has lots of pointers running around.

Now I'm seeing things like

void letter( char *somechar);
char utf8char[6];
xxx = letter((char *)&utf8char);

Back in the "old" days we would have simply said:

char utf8char[6];
xxx = letter(utf8char);

so what's the purpose of the (char *)& shenanigans?

I mean, it works, so it must be right, but I always thought that using an
array name without the subscript returns a pointer to the first element
anyway....

You're right about the behavior of the array name without a subscript,
at least most of the time. You probably knew, and just forgot to write,
that sizeof(utf8char) != sizeof(&utf8char[0]), but that's the only
non-trivial exception to the rule, IMHO.
However, you're wrong that "it works, so it must be right." While
it almost certainly *is* right, it's one of those cases that I consider
borderline: you're taking the address of an array of char[6], and then
trying to cast it to a pointer to char. I think this is guaranteed to
work, but I would never try it in real life, because who knows what
differences there might be between 'pointer to char[6]' and 'pointer
to char'?
OTOH, I do know that

xxx = letter((char *)utf8char);

will always work -- the cast in this case is purely redundant, and
won't introduce weird behavior on any system. Not to say you should
do that, of course -- your suggested solution is absolutely the right
way to do it.

xxx = letter(utf8char); /* Keep It Simple, Stupid! ;) */

-Arthur
 
E

Eric Sosman

Kamus said:
I used to do lots and lots of C coding back in the pre-ANSI days. I just
picked up a project that has lots of pointers running around.

Now I'm seeing things like

char utf8char[6];
...
xxx = letter((char *)&utf8char);

where

void letter( char *somechar) {
...
}

It might be more illuminating if you would post an
actual sample instead of "things like" what you've seen.
In the code shown the cast-and-ampersand is redundant --
but the code as shown wouldn't compile anyhow, because
letter() returns no value and can't be the RHS of an
assignment.

In other words, you may have snipped away the really
interesting part of the code, the piece that makes (or
doesn't make) this apparently useless construct necessary.
Back in the "old" days we would have simply said:

char utf8char[6];
...
xxx = letter(utf8char);

so what's the purpose of the (char *)& shenanigans?

I mean, it works, so it must be right, but I always thought that using an
array name without the subscript returns a pointer to the first element
anyway....

Yes, in almost all cases. The exceptions are `sizeof(array)'
and `& array', where you "operate on" the array as an object in
its own right instead of as a group of sub-objects.
 
R

Richard Bos

Kamus of Kadizhar said:
char utf8char[6];
...
xxx = letter((char *)&utf8char);

where

void letter( char *somechar) {
...
}

Apart from the other comments, you can't assign a void function call to
anything, because void functions don't return values. Nitpick, I know.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,355
Latest member
MavoraTech

Latest Threads

Top