473,387 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 2611
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
7
by: Nicolay Korslund | last post by:
Hi! I'm having a little trouble with the typecast operator, can anybody help me with the rules for when the this operator is invoked? In the class 'Test' in the code below, the typecast operator...
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
63
by: andynaik | last post by:
Hi, Whenever we type in this code int main() { printf("%f",10); } we get an error. We can remove that by using #pragma directive t direct that to the 8087. Even after that the output is...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
3
by: jdm | last post by:
In the sample code for the SortedList class, I see the use of a string typecasting macro consisting of a single letter "S". i.e.: Sortedlist->Add(S"Keyval one", S"Item one"); Now I have...
7
by: Raghu | last post by:
Hello All, I need some help regarding overloading operation. Is there any way to overload typecasting? I mean if i have a piece of code as below. int a = 2: float b; b = (float)a;
16
by: Abhishek | last post by:
why do I see that in most C programs, pointers in functions are accepted as: int func(int i,(void *)p) where p is a pointer or an address which is passed from the place where it is called. what...
4
by: vivekian | last post by:
Hi, This is the part of the code am trying to compile to : void Server::respondToClient ( std::string response ) { .... .... if ((numbytes = sendto ( sockFd_ , response , sizeof(response)...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.