problem with data cast
Hi everybody!!!!! I need to pass an int array through send() function and I do it in this way:
buffer = (char*)malloc(20);
buffer = (char*) array;
send(socket, buffer, sizeof(buffer), 0);
recv(socket, buf, 20, 0);
array = (int *)buf;
According to you, what's wrong? Data I receive have different values from those I send. Where is the problem?
array = (int *)buf ? Can recv() accept an integer parameter?
> buffer = (char*)malloc(20);
1. you cast malloc - this is not necessary in C - see the FAQ
2. you seem to assume that sizeof(int) is 2. If this really is the case, your compiler is too old
> buffer = (char*) array;
You just lost the memory you allocated.
> send(socket, buffer, sizeof(buffer), 0);
1. you don't check the return result.
2. sizeof() on a pointer gives you the size of the pointer, not the size of the thing it points at.
result = send(socket, array, sizeof(array), 0);
> According to you, what's wrong?
Pretty much every line of code you've written has problems.
a) learning C
b) reading all the tutorials in the announcements on this forum