[RESOLVED] about void pointers
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32

Thread: [RESOLVED] about void pointers

  1. #1
    Join Date
    Apr 2009
    Posts
    615

    [RESOLVED] about void pointers

    can anyone explain to me how can i use the void pointers?
    i understand that can recive an adress variable(&). but can recive a value?

    Code:
    int a=5;
    void *d;
    b=&a;
    b=100;//??? why i can't do these?(ok i don't remember everything about pointers, but please tell me these)

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057

    Re: about void pointers

    You didn't show the definition of b, but
    void* b = (void*)100 is legal, although probably not a good idea.

  3. #3
    Join Date
    Apr 2009
    Posts
    615

    Re: about void pointers

    Quote Originally Posted by GCDEF View Post
    You didn't show the definition of b, but
    void* b = (void*)100 is legal, although probably not a good idea.
    thanks
    i used: cout << "\n" << c;
    but i get a strange result. seems be in hexadecimal... why?

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,272

    Re: about void pointers

    Quote Originally Posted by Cambalinho View Post
    i used: cout << "\n" << c;
    but i get a strange result. seems be in hexadecimal... why?
    Of what type is c?
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Apr 2009
    Posts
    615

    Re: about void pointers

    Quote Originally Posted by 2kaud View Post
    Of what type is c?
    sorry i'm speaking about void pointers
    void *c;

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,272

    Re: about void pointers

    In this example

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    void* c = (void*)94;
    
    	cout << c << endl;
    	cout << (int)c << endl;
    	return 0;
    }
    gives the output
    Code:
    0000005E
    94
    c is a pointer to void. By default, cout displays pointers in hex. To get the output in decimal, cast the variable to an int or some other type that cout displays in decimal.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Apr 2009
    Posts
    615

    Re: about void pointers

    Quote Originally Posted by 2kaud View Post
    In this example

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    void* c = (void*)94;
    
    	cout << c << endl;
    	cout << (int)c << endl;
    	return 0;
    }
    gives the output
    Code:
    0000005E
    94
    c is a pointer to void. By default, cout displays pointers in hex. To get the output in decimal, cast the variable to an int or some other type that cout displays in decimal.
    thanks
    see my new exemple:
    Code:
    std::vector<int> b;
        b.resize(3);
        b[0]=100;
        b[1]=200;
        b[2]=300;
        c=(void *)"hello world";
    cout << "\n" <<(char *) c;
        c=&b[0];
        cout << "\n" <<*c;
    sorry what i'm doing wrong?

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057

    Re: about void pointers

    Quote Originally Posted by Cambalinho View Post
    thanks
    see my new exemple:
    Code:
    std::vector<int> b;
        b.resize(3);
        b[0]=100;
        b[1]=200;
        b[2]=300;
        c=(void *)"hello world";
    cout << "\n" <<(char *) c;
        c=&b[0];
        cout << "\n" <<*c;
    sorry what i'm doing wrong?
    If you're trying to get c to = 100, you should set it to b[0], not &b[0];

    What you're doing doesn't make a whole lot of sense though. Every post you've made so far has used variables where you don't show the declaration. Properly worded questions will get you much better answers.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,250

    Re: about void pointers

    Your compiler should be giving you an error message telling you what you are doing wrong. What is the error message and what do you think it means?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Apr 1999
    Posts
    27,423

    Re: about void pointers

    Quote Originally Posted by Cambalinho View Post
    thanks
    see my new exemple:
    If "c" is a void pointer, how is the compiler supposed to know what to do with it when you ask for it to be dereferenced?
    Code:
    c=&b[0];
    cout << "\n" << *c;
    A void pointer cannot be dereferenced unless you tell the compiler (by casting) what that void pointer is supposed to be pointing to.

    Regards,

    Paul McKenzie

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,272

    Re: about void pointers

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    vector<int> b;
    
        b.resize(3);
        b[0]=100;
        b[1]=200;
        b[2]=300;
    
    void * c=(void *)"hello world";
    
        cout <<(char *) c << endl;
        c = &b[0];
        cout << *(int*)c;
        return 0;
    }
    Produces the output
    Code:
    hello world
    100
    c is a pointer to void. To dereference the pointer and display its contents, cout needs to know to what type of data c is pointing. You used a cast for the first cout statement and you need another one for the second.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #12
    Join Date
    Apr 2009
    Posts
    615

    Re: about void pointers

    Quote Originally Posted by Paul McKenzie View Post
    If "c" is a void pointer, how is the compiler supposed to know what to do with it when you ask for it to be dereferenced?
    Code:
    c=&b[0];
    cout << "\n" << *c;
    A void pointer cannot be dereferenced unless you tell the compiler (by casting) what that void pointer is supposed to be pointing to.

    Regards,

    Paul McKenzie
    c=& b[0];
    cout << "\n" << *c;

    error message:
    "error: 'void*' is not a pointer-to-object type"

  13. #13
    Join Date
    Apr 2009
    Posts
    615

    Re: about void pointers

    thanks
    but for next array index?

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057

    Re: about void pointers

    Quote Originally Posted by 2kaud View Post
    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
    vector<int> b;
    
        b.resize(3);
        b[0]=100;
        b[1]=200;
        b[2]=300;
    
    void * c=(void *)"hello world";
    
        cout <<(char *) c << endl;
        c = &b[0];
        cout << *(int*)c;
        return 0;
    }
    Produces the output
    Code:
    hello world
    100
    c is a pointer to void. To dereference the pointer and display its contents, cout needs to know to what type of data c is pointing. You used a cast for the first cout statement and you need another one for the second.
    You're also dereferncing the pointer to get cout to produce 100. That may or may not be what the op is trying to do. Hard to tell. He's trying to change the value of the pointer, or modify the string literal that it points to. Either way, it's a bad idea.

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057

    Re: about void pointers

    Quote Originally Posted by Cambalinho View Post
    thanks
    but for next array index?
    Using words, not code, what are you trying to do?

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center