473,385 Members | 2,029 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,385 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 13075
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: 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: 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...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.