Thread: convert reference to pointer?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    46

    convert reference to pointer?

    Hi,

    I have a function that gives me a string reference:
    Code:
    int funcA (string &s)
    { ...}
    and within there I need to call a function that takes a void pointer:
    Code:
    int funcB(void *v) { ...}
    is there any way to convert the string reference to a void pointer?

    basically I want to call something like this (pass in the address of s)
    Code:
     int result = funcB( &s );

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What does funcB expect about the void pointer it is given? If it just using it as memory, then just pass in the address. If it expects say a character string, then you'll have to turn your string into a char[] or similar.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    hmm,... actually I didn't expect funcB( &s ) to work
    but I guess it does!

    I thought since s was already a reference, that taking the address of s would be some kind of a double pointer. But I guess I thought wrong

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Think of it this way: Everything you do on a reference operates as if you performed it on the variable it is referencing - including taking the address of it.
    So whether s was a reference or not, you're taking the address of some string instance somewhere.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And very, very likely, there's nothing useful funcB can do with the address.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing a pointer as a reference
    By hYph3n in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 01:45 PM
  2. Textbox
    By maxorator in forum Windows Programming
    Replies: 20
    Last Post: 09-25-2005, 10:04 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Replies: 2
    Last Post: 05-04-2003, 11:55 AM
  5. C++ pointer vs reference
    By C-Dumbie in forum C++ Programming
    Replies: 4
    Last Post: 11-04-2002, 04:08 AM