difference between [] and *

Started by
16 comments, last by browny 20 years, 8 months ago
f[10] == *(f + sizeof(char) * 10); // if f is a char array

Watch out, if sizeof(char) is not 1 then you are in serious trouble. Doing + with a pointer increases it in increments of the size of the pointer, you don''t have to do it yourself.
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...
Advertisement
Also, be very careful modifying those variables.

The first one could be a pointer to constant memory, whereas the second one could not.

char *temp = "Hello World.";
char blah[] = "Hello World.";

strtok(temp, " ,\t\n"); // Will probably crash.
strtok(blah, " ,\t\n"); // Won''t crash.
--Michael Fawcett
quote:Original post by Raloth
Watch out, if sizeof(char) is not 1

It IS always 1. period.
char is 1 byte. If you want a wide character, use wchar_t.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
quote:Original post by C-Junkie
quote:Original post by Raloth
Watch out, if sizeof(char) is not 1

It IS always 1. period.


... unless, say, you redefined char to be wchat_t, for say, internationalization ...

... or maybe some compilers that actually have internationalization support built in ...

he is trying to point out a bug in the code anyways. There shouldn''t be the sizeof statement in there at all.

aka: f[10] == *(f + 10) for any type.
but: f[10] == *(f + sizeof( type ) * 10) is incorrect.

... so no more random attacks for the hell of it ...
quote:Original post by MaulingMonkey
... unless, say, you redefined char to be wchat_t, for say, internationalization ...
Which, of course, you would NEVER do, because redefining keywords is disallowed by the standard...
quote:... or maybe some compilers that actually have internationalization support built in ...
Not that would change the size of char; this would break so much code, it''s not even funny.
quote:he is trying to point out a bug in the code anyways. There shouldn''t be the sizeof statement in there at all.

aka: f[10] == *(f + 10) for any type.
but: f[10] == *(f + sizeof( type ) * 10) is incorrect.

I''m pretty sure that SR was pointing exactly this out, implying that the compiler would automultiply by the type size. It would''ve been more clear with casts, tho:

f[10] == *(char*)((int)f + sizeof(char) * 10); // if f is a char array
quote:... so no more random attacks for the hell of it ...
But it''s sooo much fun!



How appropriate. You fight like a cow.
quote:Original post by Sneftel
quote:Original post by MaulingMonkey
... unless, say, you redefined char to be wchat_t, for say, internationalization ...
Which, of course, you would NEVER do, because redefining keywords is disallowed by the standard...
quote:... or maybe some compilers that actually have internationalization support built in ...
Not that would change the size of char; this would break so much code, it''s not even funny.

Valid points, but my original point is it''s bad to assume .
quote:
quote:he is trying to point out a bug in the code anyways. There shouldn''t be the sizeof statement in there at all.

aka: f[10] == *(f + 10) for any type.
but: f[10] == *(f + sizeof( type ) * 10) is incorrect.

I''m pretty sure that SR was pointing exactly this out, implying that the compiler would automultiply by the type size. It would''ve been more clear with casts, tho:

f[10] == *(char*)((int)f + sizeof(char) * 10); // if f is a char array

Not only more clear, but not in error for non char types, which would be the only reason for including the sizeof(char) item in the first place.

quote:
quote:... so no more random attacks for the hell of it ...
But it''s sooo much fun!

How appropriate. You fight like a cow.


* goes into a fury *

actually...yeah it IS fun .
There was a post somewhere explaining that char doesn''t have to be 1 byte by the standards. It just happens that the major compilers make it 1, and all the characters you could want fit into 1 byte. But technically it can be greater than 1...
____________________________________________________________AAAAA: American Association Against Adobe AcrobatYou know you hate PDFs...

This topic is closed to new replies.

Advertisement