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

Explanation for void *ptr=value?

1. I need explanation for void *ptr=value?

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.   void *the_data;
  4.  
  5. the_data=20;
  6. //  the_data=(int *) 20;
  7.  
  8.  printf("the_data now :%d", the_data);
  9. //  printf("the_data now :%d", *(int*) the_data);
  10.  
  11.   return 0;
  12. }
When I compile this program in gcc compiler I got the output as

the_data now:20

But I can't do the step I commented, my compiler core dumped.


1. Can a pointer variable directly point a value? How can we call it?
2. I can’t deference it like *(int*) the_data? Why?
3. As I know that void* cannot be deference directly such as *the_data,
What (the_data) pointer contains address or value?


Thanks in advance,
Saravanan.S
Mar 7 '08 #1
3 1678
Banfa
9,065 Expert Mod 8TB
1. Can a pointer variable directly point a value? How can we call it?
No a pointer cannot point to a value, it has to point to a memory location which means another variable or a string stored in constant memory (string literals a slightly different to numeric literals in that the compiler has to place them in memory somewhere).
2. I can’t deference it like *(int*) the_data? Why?
Yes you can, the reason it did not work for you is that you have not allocated any memory to your pointer. That is when you attempted to dereference it it was not pointing to a valid piece of memory and so a memory exception occured.
3. As I know that void* cannot be deference directly such as *the_data,
What (the_data) pointer contains address or value?
All pointers contain an address regardless of what type they point to.
Mar 8 '08 #2
donbock
2,426 Expert 2GB
1. I need explanation for void *ptr=value?
The gist of your program is ...

Expand|Select|Wrap|Line Numbers
  1. int *the_data;
  2. the_data = (void*)20;
  3. printf ("the_data now: %d", *the_data);
Notice that I took some liberties with int* versus void* to enhance clarity. I'm confident that my variant is equivalent to your original program.

Your program
1. Declares the_data as a pointer-to-int.
2. Sets the pointer value to "20". Perhaps this value will look more like an address if I express it in hex: 0x001C.
3. Prints the contents of this_data; that is, the contents of 0x001C.

Notice that there is no variable within your program whose address is 0x001C. That location is outside the domain of your program. What do you expect to find at that location? Perhaps a memory-mapped I/O register, or perhaps a flag set by another thread of execution?

Your program dumps core because the execution environment won't let you access locations that aren't in your program. Not all execution environments are so fastidious. What you're trying to do is not unusual in embedded systems.

One last thought -- whatever you're trying to do; address 0x001C is not written to by your program. You would therefore be strongly advised to declare this_data as follows:
Expand|Select|Wrap|Line Numbers
  1. volatile int *this-data;
The volatile type qualifier alerts the compiler that accesses to this location must occur precisely as specified in your source code -- the optimizer is not permitted to translate your source code into an equivalent layout.
Mar 10 '08 #3
donbock
2,426 Expert 2GB
1. I need explanation for void *ptr=value?

Expand|Select|Wrap|Line Numbers
  1. void *the_data;
  2. the_data=(int *) 20;
  3. printf("the_data now :%d", *(int*) the_data);
  4.  
1. Can a pointer variable directly point a value? How can we call it?
2. I can’t deference it like *(int*) the_data? Why?
3. As I know that void* cannot be deference directly such as *the_data,
What (the_data) pointer contains address or value?


Thanks in advance,
Saravanan.S
From your question #1 I suspect you really meant ...
Expand|Select|Wrap|Line Numbers
  1. int the_data = 20;
  2. int *the_data_ptr = &the_data;
  3. printf("the_data now :%d", *the_data_ptr);
A pointer doesn't "pointer directly to a value"; it can only point to a memory location. However, that memory location can [will] contain a specific value.
Mar 10 '08 #4

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

Similar topics

4
by: Martin Jensen | last post by:
Hi First of, I know that void* usually is a bad idea to use, but I still want to figure out this problem. I assign the adress of a value to the void* and tries to retrive it again. When I try...
13
by: Andy | last post by:
Hi, Sorry for the naive question. What I did here is to check the value of pointers to member functions, and try to call these member function via member functio pointers. But the program...
15
by: Stig Brautaset | last post by:
Hi group, I'm playing with a little generic linked list/stack library, and have a little problem with the interface of the pop() function. If I used a struct like this it would be simple: ...
6
by: MackS | last post by:
Hi FAQ 4.9 states that void** cannot be used portably "to pass a generic pointer to a function by reference". I would like to hear from you whether there is anything wrong with the following...
7
by: sunglo | last post by:
My doubt comes from trying to understand how thread return values work (I know, it's off topic here), and I'm wondering about the meaning of the "void **" parameter that pthread_join expects (I...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
56
by: maadhuu | last post by:
hello, this is a piece of code ,which is giving an error. #include<stdio.h> int main() { int a =10; void *p = &a; printf("%d ", *p ); //error....why should it //be an error ?can't the...
18
by: ReaperUnreal | last post by:
I've got a strange problem here. I need to take a value that I have written down on a sticky-note here, and find what's at that address in current memory. I've tried the following: void *value =...
8
by: brad2000 | last post by:
I was doing a little bit of reading in the ISO C spec. about typecasting to a void type. This caused me to have a question. In particular, I'm curious to know about section 6.3.2.2 where the specs...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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...

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.