468,278 Members | 1,566 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,278 developers. It's quick & easy.

Typecasting

Say I have these variables:

char *array[2];
void *p;

how can I typecast p to the type of array?
Nov 14 '05 #1
11 2307
"Martin Johansen" <ma******@is.online.no> wrote:
Say I have these variables:

char *array[2];
void *p;

how can I typecast p to the type of array?


p is a void *. One does not need to cast void *s.

Richard
Nov 14 '05 #2

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
"Martin Johansen" <ma******@is.online.no> wrote:
Say I have these variables:

char *array[2];
void *p;

how can I typecast p to the type of array?


p is a void *. One does not need to cast void *s.


Well, in this example I do, so that when array is passed as a void pointer,
then casted, the compiler can still warn me of possible access violations,
and also so I can use sizeof, and also access the array directly. So I do
need to typecast this, but I cannot see how it can be done...
Nov 14 '05 #3
Martin Johansen wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
"Martin Johansen" <ma******@is.online.no> wrote:
> Say I have these variables:
>
> char *array[2];
> void *p;
>
> how can I typecast p to the type of array?


p is a void *. One does not need to cast void *s.


Well, in this example I do


You can't. C only allows casts to scalar types (or void). Array
types aren't scalar.

Jeremy.
Nov 14 '05 #4
"Martin Johansen" <ma******@is.online.no> wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
"Martin Johansen" <ma******@is.online.no> wrote:
Say I have these variables:

char *array[2];
void *p;

how can I typecast p to the type of array?


p is a void *. One does not need to cast void *s.


Well, in this example I do, so that when array is passed as a void pointer,
then casted, the compiler can still warn me of possible access violations,


I fail to see what you could possibly achieve here. Perhaps you should
demonstrate what you mean with a simpler type, say an int *.

In any case, you cannot assign to an array, so you cannot cast pointers
_to_ array types.

Richard
Nov 14 '05 #5
Jeremy Yallop wrote:

Martin Johansen wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
"Martin Johansen" <ma******@is.online.no> wrote:

> Say I have these variables:
>
> char *array[2];
> void *p;
>
> how can I typecast p to the type of array?

p is a void *. One does not need to cast void *s.


Well, in this example I do


You can't. C only allows casts to scalar types (or void).
Array types aren't scalar.


Typecasting, is something that happens to thespians.

--
pete
Nov 14 '05 #6
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
"Martin Johansen" <ma******@is.online.no> wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
"Martin Johansen" <ma******@is.online.no> wrote:

> Say I have these variables:
>
> char *array[2];
> void *p;
>
> how can I typecast p to the type of array?

p is a void *. One does not need to cast void *s.
Well, in this example I do, so that when array is passed as a void pointer, then casted, the compiler can still warn me of possible access

violations,
I fail to see what you could possibly achieve here. Perhaps you should
demonstrate what you mean with a simpler type, say an int *.
No, the point is the array.

I could solve this problem by defining a structure with only an array inside
of the array type, it would work. But is it possible to typecast directly
back to the type I refered to?

e.g.

typedef struct{
char *array[2];
}arraytype;

(arraytype)p;

would work.
In any case, you cannot assign to an array, so you cannot cast pointers
_to_ array types.

Richard

Nov 14 '05 #7
"Martin Johansen" <ma******@is.online.no> wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
I fail to see what you could possibly achieve here. Perhaps you should
demonstrate what you mean with a simpler type, say an int *.
No, the point is the array.


Well, _my_ point is that I don't see what advantage the cast brings you.
I could solve this problem by defining a structure with only an array inside
of the array type, it would work. But is it possible to typecast directly
back to the type I refered to?

In any case, you cannot assign to an array, so you cannot cast pointers
_to_ array types.


*cough* ^

Richard
Nov 14 '05 #8
Martin Johansen wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
"Martin Johansen" <ma******@is.online.no> wrote:
Say I have these variables:

char *array[2];
void *p;

how can I typecast p to the type of array?


p is a void *. One does not need to cast void *s.


Well, in this example I do,
so that when array is passed as a void pointer,
then casted, the compiler can still warn me of possible access
violations, and also so I can use sizeof,
and also access the array directly. So I do
need to typecast this, but I cannot see how it can be done...


When an array is passed as an argument to a function
for a parameter of type pointer to void,
the only information retained in the parameter,
is the address of the array.

The sizeof the elements, and their quantity,
must be passed as separate information.

--
pete
Nov 14 '05 #9
Martin Johansen wrote:
Say I have these variables:

char *array[2];
void *p;

how can I typecast p to the type of array?

#include <stdio.h>

int main(void)
{
char *array[2] = { "foobar", "horsecrap" };
void *p;
size_t i;

p = array;
printf("'array' is at %p, p points to %p\n", (void *) array, p);

for (i = 0; i < sizeof array / sizeof *array; i++)
printf("array[i] is \"%s\",\n"
"((char **)p)[i] is \"%s\".\n", array[i],
((char **) p)[i]);
return 0;
}
'array' is at effd0, p points to effd0
array[i] is "foobar",
((char **)p)[i] is "foobar".
array[i] is "horsecrap",
((char **)p)[i] is "horsecrap".
--
Martin Ambuhl
Nov 14 '05 #10
Martin Johansen wrote:
Say I have these variables:

char *array[2];
void *p;

how can I typecast p to the type of array?


This is the closest I can think of.

*(char* (*)[2])&p

That casts a pointer to p to a pointer to an array of two char*'s, and
then dereferences it.

Needless to say, this invokes UB.

--
Pull out a splinter to reply.
Nov 14 '05 #11
On Thu, 12 Feb 2004 12:52:47 +0100, "Martin Johansen"
<ma******@is.online.no> wrote:

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:40*****************@news.individual.net...
"Martin Johansen" <ma******@is.online.no> wrote:
Say I have these variables:

char *array[2];
void *p;

how can I typecast p to the type of array?


p is a void *. One does not need to cast void *s.


Well, in this example I do, so that when array is passed as a void pointer,
then casted, the compiler can still warn me of possible access violations,
and also so I can use sizeof, and also access the array directly. So I do
need to typecast this, but I cannot see how it can be done...


I don't see what you want to achieve.

Of course you could "pass" the array like this and cast p inside the
function to a pointer to array of 2 char*

void f( void*p )
{
char* (*q) [2] = p;
(*q)[1] = 0;
}

char *a[2];

f(&a);

but the code generated will be identical to the code generated for

void f( void*p )
{
char **q = p;
q[1] = 0;
}

char *a[2];

f(a);

--
Horst

Nov 14 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kapil Khosla | last post: by
7 posts views Thread by Nicolay Korslund | last post: by
63 posts views Thread by andynaik | last post: by
11 posts views Thread by Vinod Patel | last post: by
7 posts views Thread by Raghu | last post: by
4 posts views Thread by vivekian | last post: by
12 posts views Thread by bwaichu | last post: by
1 post views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.