Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old Jan 2nd, 2007, 4:17 AM   #1
Soulstorm
Hobbyist Programmer
 
Soulstorm's Avatar
 
Join Date: Jan 2006
Location: Menidi, Athens, Greece
Posts: 243
Rep Power: 3 Soulstorm is on a distinguished road
Void Pointers Question about dereferencing

I have been experimenting with void pointers. I have encountered many problems, since I want to implement them into a very big project, but I made some examples that I believe that if I understand them, I will be able to use them efficiently.

Here is my first example:
cpp Syntax (Toggle Plain Text)
  1. int main (int argc, char * const argv[]) {
  2. int *p;
  3. int a = 100;
  4. void *v;
  5. p = &a;
  6. v = p;
  7. std::cout << v;
  8. return 0;
  9. }

The result I am getting is '0xbffff9c0' from the cout function. Actually, I had predicted it, but I don't know how can I access the contents of the void pointer. Can you help?

Another example:

cpp Syntax (Toggle Plain Text)
  1. void f(void *p){
  2. printf("%s",p);
  3. }
  4.  
  5. int main (int argc, char * const argv[]) {
  6. char p[] = "hello!";
  7. f(p);
  8. return 0;
  9. }
The above code works as expected. It shows 'hello'. But if I replace the printf fuunction with the cout:
cpp Syntax (Toggle Plain Text)
  1. void f(void *p){
  2. cout << p;
  3. }
  4.  
  5. int main (int argc, char * const argv[]) {
  6. char p[] = "hello!";
  7. f(p);
  8. return 0;
  9. }
I get an address! (0xbffff9b8).

Can you please explain to me why this is happenning (in both examples) and what can I do to dereference a void pointer so that I can access the elements it points to?
__________________
Project::Soulstorm (personal homepage)
Soulstorm is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Appreciate comments on some linked list code Jessehk C 5 Jul 20th, 2006 8:59 PM
Pointers in C (Part II) Stack Overflow C 2 Apr 29th, 2005 11:39 AM
Pointers in C (Part I) Stack Overflow C 4 Apr 28th, 2005 8:03 PM
Help! I have a problem thomas55 C++ 1 Apr 24th, 2005 2:16 PM
function solomon_13000 Java 6 Apr 3rd, 2005 12:42 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC