type casting in C++

This is a discussion on type casting in C++ within the Networking/Device Communication forums, part of the General Programming Boards category; Hi At the moment I'm ill so I'm passing time by doing this so maybe in my current state of ...

  1. #1
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    type casting in C++

    Hi

    At the moment I'm ill so I'm passing time by doing this so maybe in my current state of mind there's something I'm overlooking.

    I have a class which stores the variables needed to construct a simple client socket the address is stored as a C++ string. I keep getting compiler error messages saying it can't convert from string to char * even though I'm casting
    Code:
     ServerAdd.sin_addr.s_addr = inet_addr((char *)address);
    what am I doing wrong here?
    Thank you alll for your time.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't think there is an automatic translation available for string to char *. You could use
    Code:
    address.c_str()
    if you just need to make a string into a char array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Code:
    LPVOID lpTemp;
     
    lpTemp = (LPVOID)address;
     
    ServerAdd.sin_addr.s_addr = inet_addr((char *)lpTemp);
    you can always recast to/from type void*
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abachler View Post
    Code:
    LPVOID lpTemp;
     
    lpTemp = (LPVOID)address;
     
    ServerAdd.sin_addr.s_addr = inet_addr((char *)lpTemp);
    you can always recast to/from type void*
    Yes, but that will give you the memory-address of the string object address, which doesn't give you the memory-address of the characters in the string (at least, that's far from guaranteed).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    oh wait, i reread the original post, seems I missed the fact that he is directly using the string object.

    this code will fix the problem -
    Code:
     ServerAdd.sin_addr.s_addr = inet_addr((char *)&address);
    unless 'address' is already a pointer, in which case my previous post stands.
    Last edited by abachler; 10-10-2007 at 09:12 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  6. #6
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Thank you for your replies. I think the last post worked but however I threw up another error which maybe or maynot be related. I keep getting
    Undefined reference to htons@4
    Which probably may or maynot happen if I'd stayed programming on a *nix system.
    I thought the "windsock.h" file included all the socket programming headers I needed? #including<netinet/in.h> doesn't resolve this.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    MS htons page says that it's winsock2.h ...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    that looks like a link time error, are you linking the appropriate winsock LIB?
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  9. #9
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    No joy. Still throwing the same error. Compiled with Winsock and winsock2.h header files.
    BTW thought I should mention that I'm using Dev C++ and I did search for that library file (Ws2_32.lib) on windows but it windows search turned up nothing.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  10. #10
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Dev c++ doesnt use the LIB extension, i think they use the .a extension to denote static libraries. I know at least in VS that you have to include the winsock header BEFORE you include windows.h or else it throws all kinds of errors.

    Update -

    I found the file they use under Dev C++ , its libws2_32.a

    include that in the link and that should fix the problem.
    Last edited by abachler; 10-15-2007 at 10:50 PM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What dose this type casting mean??
    By zhoufanking in forum C Programming
    Replies: 4
    Last Post: 06-11-2008, 07:09 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. help with simple type casting problem
    By Jeremy_S in forum C Programming
    Replies: 2
    Last Post: 02-27-2002, 12:38 PM

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