Typecasting

R

Richard Bos

Martin Johansen said:
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
 
M

Martin Johansen

Say I have these variables:

char *array[2];
void *p;

how can I typecast p to the type of array?
 
R

Richard Bos

Martin Johansen said:
Richard Bos said:
Martin Johansen said:
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
 
M

Martin Johansen

Richard Bos said:
Martin Johansen said:
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...
 
J

Jeremy Yallop

Martin said:
Richard Bos said:
Martin Johansen said:
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.
 
P

pete

Jeremy said:
Martin said:
Richard Bos said:
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.
 
M

Martin Johansen

Richard Bos said:
Martin Johansen said:
Richard Bos said:
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.
 
R

Richard Bos

Martin Johansen said:
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?

*cough* ^

Richard
 
P

pete

Martin said:
Richard Bos said:
Martin Johansen said:
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.
 
M

Martin Ambuhl

Martin said:
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 is \"%s\",\n"
"((char **)p) is \"%s\".\n", array,
((char **) p));
return 0;
}


'array' is at effd0, p points to effd0
array is "foobar",
((char **)p) is "foobar".
array is "horsecrap",
((char **)p) is "horsecrap".
 
P

Peter Ammon

Martin said:
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.
 
H

Horst Kraemer

Richard Bos said:
Martin Johansen said:
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);
 

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,807
Members
47,356
Latest member
Tommyhotly

Latest Threads

Top