473,231 Members | 1,503 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,231 software developers and data experts.

dereferencing void pointers by typecasting

Hello everyone,

I am having a problem with typecasting void pointers.I have read the pointer basics but still cant understand why the following test code doesnt work.

void *xyz;

struct abcd
{
int a;
};

struct abcd a,*ptra;
ptra=&a;
xyz=(struct abcd *)ptra; //doesnt work but no error shown.
.
.
printf("%d \n",xyz->a);

}

warning: dereferencing void * pointer (obviously,typecasting didnt work)

error : request for member a in something not a structure or union;

Am i missing some pointer concepts here?


Thanks,
Sritej
Nov 2 '08 #1
3 13045
newb16
687 512MB
Hello everyone,

I am having a problem with typecasting void pointers.I have read the pointer basics but still cant understand why the following test code doesnt work.

void *xyz;

struct abcd
{
int a;
};

struct abcd a,*ptra;
ptra=&a;
xyz=(struct abcd *)ptra; //doesnt work but no error shown.
.
.
printf("%d \n",xyz->a);

}
Information about structure type is not stored in pointer, pointer is just an address.
So when you write xyz->a compiler has no idea on what ->a is and what is its offset within memory block pointed by xyz

Expand|Select|Wrap|Line Numbers
  1. ptra=&a;
  2. xyz=ptra; // no need to typecast here
  3. printf("%d \n",((struct abcd *)xyz)->a);
  4.  
where (struct abcd *)xyz is of type "struct abcd*" and can be used as an argument to ->
Nov 2 '08 #2
thanks a lot for the correction..
so initial declaration of pointer is taken into account while dereferencing so that offsets can be computed .Thats why typecasting is done while dereferencing.

Thanks,
Sritej
Nov 2 '08 #3
AmeL
15
Expand|Select|Wrap|Line Numbers
  1. void *xyz;
  2. struct abcd
  3. {
  4. int a;
  5. };
  6. struct abcd a,*ptra;
  7. ptra=&a;
  8. xyz=(struct abcd *)ptra; //doesnt work but no error shown.
  9. .
  10. .
  11. printf("%d \n",xyz->a);
  12. }
  13.  
Let review your code a bit more!!!
You declared xyz as a pointer to void.
Then after you do like :
xyz = (struct abcd *)ptra; that ptra's type is a pointer to struct abcd.
This instruction means you're trying to typecast from struct abcd pointer to struct abcd pointer ( again ) and trying to assign that to xyz that was declared as pointer to void.

Thanks
/AmeL
Nov 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Vivek | last post by:
Hi, I am trying to get as much information on void pointers. How do we take help of void pointers in writing generic functions. I have seen this kind of code for many years, but now I have got...
4
by: Pushkar Pradhan | last post by:
I have some functions which take as i/p a buffer (it can be float, char, or 16 bit, int etc.). The result is another o/p buffer, its type is also flexible (it could be a float, char etc.). I try...
3
by: sam | last post by:
Hello whats the use of void pointers? and when they are useful (on which conditions?) Please give me example code with some explanation. Thanks in advance.
5
by: Rahul | last post by:
Hi Everyone, There was a discussion on the need of void * in C and C++. In C, it is a generic pointer which can be typecasted to and from that of other types. And it is developer's head ache...
18
by: bcpkh | last post by:
Hello All Hope someone can help me, please note that at first this might look as if it is posted to the wrong group but if you ignore the specifics I think it is general pointer referencing...
160
by: raphfrk | last post by:
Is this valid? int a; void *b; b = (void *)a; // b points to a b += 5*sizeof(*a); // b points to a a = 100;
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.