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

assigning to void pointers

63
main() {
void *poin;
unsigned int value;
value =1000;
printf("%u\n",value);
*(unsigned int *)poin=(unsigned int)value;
printf("%u\n",*poin);

}


is my way of assigning a value to convert void ppointer to any type is wrong...
pls help
May 10 '07 #1
2 9884
pradeep kaltari
102 Expert 100+
main() {
void *poin;
unsigned int value;
value =1000;
printf("%u\n",value);
*(unsigned int *)poin=(unsigned int)value;
printf("%u\n",*poin);

}


is my way of assigning a value to convert void ppointer to any type is wrong...
pls help
Hi,
The assignment to the void pointer doesn't have any problem but the way you are trying to print the value using the pointer is incorrect.

To need to cast the pointer to unsigned integer pointer and then access the value contained in the location to which it is pointing.
Try the following:
Expand|Select|Wrap|Line Numbers
  1. void *poin;
  2. unsigned int value;
  3. value =1000;
  4. printf("%u\n",value);
  5. *(unsigned  int *)poin=(unsigned int)value;
  6. printf("%u\n",*(unsigned int *)poin);
  7.  
I hope this helps you.

Regards,
Pradeep
May 10 '07 #2
Banfa
9,065 Expert Mod 8TB
main() {
void *poin;
unsigned int value;
value =1000;
printf("%u\n",value);
*(unsigned int *)poin=(unsigned int)value;
printf("%u\n",*poin);

}


is my way of assigning a value to convert void ppointer to any type is wrong...
pls help
Actually this is a big problem, you have dereferenced the pointer poin without actually pointing it at any memory.

poin will have a random value when you enter main so you could be writing anywhere in memory.

Your casting of the void pointer is OK but you need to allocate the pointer some memory before copying anything to it.
May 12 '07 #3

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

Similar topics

37
by: Dave | last post by:
Hello all, Please consider the code below. It is representative of a problem I am having. foo_t needs to contain a bar_t which is a class without a copy constructor or operator=. It is not...
5
by: trying_to_learn | last post by:
Hello All Have another qn from the practice exercises. I ve done the exercise and the birds fly member fn was called successfully. But i still cant answer the authors qn. why this ability to...
27
by: Erik de Castro Lopo | last post by:
Hi all, The GNU C compiler allows a void pointer to be incremented and the behaviour is equivalent to incrementing a char pointer. Is this legal C99 or is this a GNU C extention? Thanks in...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
17
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void...
25
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
8
by: mast2as | last post by:
I almost apologize to ask this question but I have been starting at this code for a bit of time and don't understand what's wrong with it. I am not arguing about the fact it's good or not coding,...
3
by: Donos | last post by:
Hello I have the following program, void getValue(unsigned char* pVal) { pVal = allValues(); } unsigned char* allValues()
43
by: emyl | last post by:
Hi all, here's an elementary question. Assume I have declared two variables, char *a, **b; I can then give a value to a like a="hello world";
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.