Velocity Reviews > Casting char* to int*

# Casting char* to int*

Alex Vinokur
Guest
Posts: n/a

 11-16-2007
char* pch [120];

// Should we check
// if (((size_t) pch & 3) == 0)
// before the following statement ?
int* pint = (int*) pch;

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Richard Heathfield
Guest
Posts: n/a

 11-16-2007
Alex Vinokur said:

> char* pch [120];
>
> // Should we check
> // if (((size_t) pch & 3) == 0)
> // before the following statement ?
> int* pint = (int*) pch;

The cast is meaningless, as is the assignment.

Don't Do That.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Chris Dollin
Guest
Posts: n/a

 11-16-2007
Alex Vinokur wrote:

> char* pch [120];
>
> // Should we check
> // if (((size_t) pch & 3) == 0)
> // before the following statement ?
> int* pint = (int*) pch;

No, we simply shouldn't do the assignment. Or, for that matter, the if.
Come to think of it, calling a variable `pch` looks a bit odd. I assume
that `pint` is a variable to cope with transAtlantic variation?

--
Chris "drinks halves anyway" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Alex Vinokur
Guest
Posts: n/a

 11-16-2007
On Nov 16, 11:32 am, Alex Vinokur <(E-Mail Removed)>
wrote:
> char* pch [120];
>
> // Should we check
> // if (((size_t) pch & 3) == 0)
> // before the following statement ?
> int* pint = (int*) pch;
>

Sorry, should be:

char pch [120];

// Should we check
// if (((size_t) pch & 3) == 0)
// before the following statement ?
int* pint = (int*) pch;

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Richard Heathfield
Guest
Posts: n/a

 11-16-2007
Alex Vinokur said:

> On Nov 16, 11:32 am, Alex Vinokur <(E-Mail Removed)>
> wrote:
>> char* pch [120];
>>
>> // Should we check
>> // if (((size_t) pch & 3) == 0)
>> // before the following statement ?
>> int* pint = (int*) pch;
>>

>
> Sorry, should be:
>
> char pch [120];
>
> // Should we check
> // if (((size_t) pch & 3) == 0)
> // before the following statement ?
> int* pint = (int*) pch;

As you can see, your correction has a remarkable effect on my reply, which
becomes:

The cast is meaningless, as is the assignment.

Don't Do That.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999

Chris Dollin
Guest
Posts: n/a

 11-16-2007
Alex Vinokur wrote:

> On Nov 16, 11:32 am, Alex Vinokur <(E-Mail Removed)>
> wrote:
>> char* pch [120];
>>
>> // Should we check
>> // if (((size_t) pch & 3) == 0)
>> // before the following statement ?
>> int* pint = (int*) pch;
>>

> Sorry, should be:
>
> char pch [120];
>
> // Should we check
> // if (((size_t) pch & 3) == 0)
> // before the following statement ?
> int* pint = (int*) pch;

Same answer: don't /do/ that. Why would you want to? If you want to
initialise a pointer-to-int, how about initialising it with the

[PS what were you going to do if your suspect conditional wasn't true?]

--
Chris "oh noes! characters isn't integers! (fx:dies)" Dollin

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Mark Bluemel
Guest
Posts: n/a

 11-16-2007
Alex Vinokur wrote:

> char pch [120];
>
> // Should we check
> // if (((size_t) pch & 3) == 0)
> // before the following statement ?
> int* pint = (int*) pch;

What are you trying to achieve? If you want to handle ints, why have a
byte array?

Oh, and why are you crossposting - which language do you want to use?

(I'm only going to reply in comp.lang.c)

Joe Wright
Guest
Posts: n/a

 11-17-2007
Alex Vinokur wrote:
> char* pch [120];
>
> // Should we check
> // if (((size_t) pch & 3) == 0)
> // before the following statement ?
> int* pint = (int*) pch;
>

pch is not a char pointer. It is an array [120] of pointers to char.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---

pete
Guest
Posts: n/a

 11-17-2007
Joe Wright wrote:
>
> Alex Vinokur wrote:
> > char* pch [120];
> >
> > // Should we check
> > // if (((size_t) pch & 3) == 0)
> > // before the following statement ?
> > int* pint = (int*) pch;

That would be both a declaration and an object definition,
but the above line of code, is not a statement.

> >

>
> pch is not a char pointer. It is an array [120] of pointers to char.

And, prior to being operated on by each cast operator,
the expression (pch),
is implicitly converted to type (char **).

I think Alex is depending on some intimate knowledge
of a specific C implementation.

According to the rules of C, whether or not
int* pint = (int*) pch;
will work right, depends on size and alignment issues.

It *cannot* be construed from the C standard, that
if (((size_t) pch & 3) == 0)
is sufficient to check for those size and alignment issues.

--
pete

Alex Vinokur
Guest
Posts: n/a

 11-19-2007
On Nov 16, 12:25 pm, Richard Heathfield <(E-Mail Removed)> wrote:
> AlexVinokursaid:
>
>
>
>
>
> > On Nov 16, 11:32 am,AlexVinokur<(E-Mail Removed)>
> > wrote:
> >> char* pch [120];

>
> >> // Should we check
> >> // if (((size_t) pch & 3) == 0)
> >> // before the following statement ?
> >> int* pint = (int*) pch;

>
> > Sorry, should be:

>
> > char pch [120];

>
> > // Should we check
> > // if (((size_t) pch & 3) == 0)
> > // before the following statement ?
> > int* pint = (int*) pch;

>
> As you can see, your correction has a remarkable effect on my reply, which
> becomes:
>
> The cast is meaningless, as is the assignment.
>
> Don't Do That.

The casting or the assignment?

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn