Hugo Mota
Hugo Mota

Reputation: 11577

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 third argument to my function receive_message().

thank you.

Upvotes: 1

Views: 193

Answers (4)

CB Bailey
CB Bailey

Reputation: 792487

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.

Upvotes: 2

Drakosha
Drakosha

Reputation: 12165

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

Upvotes: 1

Peter Alexander
Peter Alexander

Reputation: 54290

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.

Upvotes: 1

geekosaur
geekosaur

Reputation: 61439

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

Upvotes: 10

Related Questions