pointer question

This is a discussion on pointer question within the C++ Programming forums, part of the General Programming Boards category; Code: char c[6]={"hello"}; void* data; data=c; Is there any way I can get c[4] or c[3] by using data? I ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    51

    pointer question

    Code:
    
    char c[6]={"hello"};
    
    void* data;
    
    data=c;
    Is there any way I can get c[4] or c[3] by using data?
    I tried cout<< (char*)data[4]; but it doesnt work.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,628
    You need to convert the void pointer back to an appropriate type (char *), BEFORE trying to dereference it.
    Code:
         char *cdata = (char *)data;
         cout << cdata[4];
    or (if you don't want to add another variable)
    Code:
        cout << ((char *)data)[4];
    The extra brackets I have in this line (which you do not) are important: your code and mine mean different things.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    Thx it works but it doesnt at the same time.

    I have this
    Code:
    display(const char *str){
    cout <<str;}
    
    at first, I couldnt pass c[0] or c[1] because they are const char so I thought it would work if I pass in a pointer.
    after use your code, I tried to call display
    display(((char *)data)[4]);
    it didnt work.
    is there any way I can pass c[0] to the function display?

    I need something to convert a constant char to a string ----> dam it, convert to string doesnt work lol
    Last edited by byebyebyezzz; 11-13-2011 at 06:47 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    What is c?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    oh it continues with the code I have in the 1st post.
    Code:
    char c[6]={"hello"};

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Ah. If you want to print a character, then print it. There's no need to try and force it to be printed as a null terminated string.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    well I wish it could be done that easily.
    This is a part of my assignment.
    The display function is already in used to print a null terminated string.
    I cant modify the display function.
    I cant use cout<< either. I must use that display function

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Don't modify it then. Just don't use it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    The display function isnt that simple. There are other parameters that I didnt mention here.
    I need to use that display function to display something like
    (hello world)
    where
    hello world is the null terminated string
    ( ) are store in a char c[3];
    c[3] could hold anything, [], //...
    w/o using the display function, no way I can get them to display together unless I write an overload function for display which I am not allowed to.

    I can call display("(") w/o any problem but the thing is that it is not always a "(".
    Last edited by byebyebyezzz; 11-13-2011 at 08:53 PM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    You could modify the null terminated string to prepend '(' and append ')'. Or you could std::cout << '('; call this function, then std::cout << ')';
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    could you tell me more on how can I prepend '(' to a null terminated string?

    thanks

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    Quote Originally Posted by byebyebyezzz
    could you tell me more on how can I prepend '(' to a null terminated string?
    There are a few ways, e.g.,
    Code:
    stringstream ss;
    ss << c[0] << str << c[1];
    std::strcpy(str, ss.str().c_str()); // assume enough space for strcpy
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    well this what I did and it worked

    char s[2]

    s[0] = c[0];
    s[1] = '\0';

    I wouldnt come up with that w/o you

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  2. pointer question.... pointer theory?
    By panfilero in forum C Programming
    Replies: 6
    Last Post: 11-12-2005, 02:29 AM
  3. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 11:19 PM
  4. pointer to pointer as argument question
    By Lateralus in forum C Programming
    Replies: 4
    Last Post: 07-21-2005, 06:03 PM
  5. Pointer to pointer question
    By caduardo21 in forum C Programming
    Replies: 7
    Last Post: 07-18-2005, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21