开发者

how can i get the size of my vector in C?

I have this function:

void receive_message(int sock, char buffer[]) {

    int test = recv(sock, buffer, strlen(buffer), 0);
    buffer[test] = '\0';
}

the third argument of the function recv() is not working. apparently i cannot use strlen() because the buffer don't have a \0. sizeof() didn't help me either. i'm wishing i can do this without passing a thir开发者_如何学Cd argument to my function receive_message().

thank you.


You're hoping in vain; C arrays don't have that much structure. You need to pass the size yourself.


buffer isn't a vector. It might look like an array, but as it's declared as a function argument it's actually a pointer. There's no way to know how long a buffer pointed to by a pointer is unless you know it is terminate with a sentinel value (such as \0).

It's probably easiest to let the function take an additional parameter.


There is no way to get the size if it isn't zero terminated. You have no choice but to pass in the size as an argument or zero-terminate the string.


You cannot do it. Pass the extra parameter, or use a #define in buffer definition and in the use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜