# char* question

This topic is 4706 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i have two questions but they are pretty simple. the code is for a dll i'm making and its kind of hard to see what the problem with the code is because of the language i'm making it for.
export double sock_connect(double port, char *ipaddr)
{

// ...
}
first, since i have to pass the port number as a double, stat_cast<int>(port) won't give me a different number than what i actually passed from the exe will it? second, i think the ipaddr is not working correctly. i never really understood how you can write cout << charP; and it would output the whole string, i'm guessing that since it will do that, that inet_addr(ipaddr) will get the whole string and not just the first character in the string. thank you for your time.

##### Share on other sites
why does the port have to be a double. thats just wrong. ports are always integers.

if for some reason you _have_ to use a double( and it better be good :P ) just a c-style cast will preserve the number as it best can

int x = (int)someDoubleValue;

cout << someCharPointer works because there is the following function somewhere in trhe iostream include

ostream& operator<<( ostream &, const char * );

which expects that the char* points to a nul terminated string of characters.

inet_addr expects exactly the same thing.

##### Share on other sites
What is the need for passing a port number (which is in fact an integer in the range 0/1 to 65535) as a double??

I assume all of the following
(int)port // old style C cast
static_cast<int>(port) // new style C++ cast
int(port) // "initializer"
will cast the double down to an int by stripping off the fractional part.

char *ipaddr denotes a pointer to a char. When interpreted by inet_addr, the routine not only uses that one char but also all following up until a '\0' character is encountered. So actually you don't pass a single character only but a "zero terminated character sequence".

EDIT: Ehh, a few seconds after rip-off. Nevertheless...

##### Share on other sites
i already knew that you couldn't do anything socket-wise until you called WSAStartup(), but if you wouldn't have said anything i probably would never have found the problem. thank you all for your help.

##### Share on other sites
Quote:
 Original post by haegarrint(port) // "initializer"

not quite.

int someVariable( someDoubleValue ); is an initialiser.

int( someDoubleValue ) is a c style cast.

there are two forms of c-style cast

(new_type)expression

new_type( expression )

the second one isnt used often. it looks like a function call, so i thinks its called functional style or some such. a bit like sizeof.

##### Share on other sites
Nope; the functional-style int(foo) cast does not exist in C. It is a C++ addition, and should IMO be preferred over the C-style (int)foo cast. The syntax mirrors the constructor syntax, after all, that's what we are doing - constructing a temporary int variable from some value.

##### Share on other sites
@rip-off
Don't know the real name of that construct, hence I've used the double quotes.

However, I'm not sure whether you're right. Trying a
int a = int(0);
in C lets gcc complain a syntax error. In C++ mode all is okay. Or do you mean C++ shows an alternative C style cast (if so, why the hell should it)?

I assumed it being an equivalent to an anonymous object initialization, like
Vector3d(0,0,0)
may be used to invoke the matching c'tor of a 3D vector class (just to have an example at hand) without giving it a name.

The mentioned term
int a(0);
is IMHO a valid initialization of a named (say not anonymous) variable.

Okay, in fact the functionality behind that stuff is IMHO in both cases the same, so it is an academic question only?!

EDIT: Damned. Second time beaten in the same thread ;-)

##### Share on other sites
Quote:
 Original post by SharlinNope; the functional-style int(foo) cast does not exist in C. It is a C++ addition, and should IMO be preferred over the C-style (int)foo cast. The syntax mirrors the constructor syntax, after all, that's what we are doing - constructing a temporary int variable from some value.

i never said it could be used in c, i called it c-style as it mimics how c casts act.

hehe, i lie. i thought you could use it in c. it was just fortunate that my wording allowed me to make up such a statement =)

##### Share on other sites
Quote:
 Original post by rip-offhehe, i lie.

LIES, LIES AND DECEPTION MY FRIEND!!!

Sorry, that's just like, one of my favorite phrases. I had to.

Quote:
 double port

...

Use an integer type. Please! That just feels so wrong on so many levels it makes my skin itch. It's really of little importance, but it's really rather silly to be forcing yourself to do those casts and whatnot just to supress warnings.

Plus, someone will eventually try to use your code with the port "5000.5", and then submit a bug report when it connects to port 5000. Then you'll have to emulate ports outside the 1-65535 range (and with greater-than-integer precision) over a custom protocol, and that's just entering nastyland. You'll then also break older programs like webbrowsers using the port "80.1" just to poke fun at your library. It's more a matter of self-documentation than of preformance or anything like that, in this case anyways.

1. 1
2. 2
3. 3
Rutin
20
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633655
• Total Posts
3013180
×