converting 2d char array to char*

109 Views Asked by At

I am working with WinSock , i need to send a 2D char array. for example

    char SendBuf[10][1024];
for (int i = 0; i < 10; i++){
    fgets(SendBuf[i], sizeof(SendBuf), stdin);
}

and than casted it to (char*).

 iResult = sendto(SendSocket, (char*)SendBuf, BufLen, 0, (SOCKADDR *)& RecvAddr, sizeof(RecvAddr));

Everything works. But on the server side i am just getting only the value of Sendbuf[0][1024], what should i do to read whole buffer.

2

There are 2 best solutions below

0
On BEST ANSWER

Should work with:

const int Buflen = 10*1024;

and note that string N will be at location RecBuf[N*1024]

1
On

I can't comment here yet, but looking at your last comment I think your BufLen is the problem. If you're only sending 1024 bytes, then that's why you're only receiving 1024 bytes. You need your BufLen to be 10*1024*1.

"char SendBuf[10][1024]" is going to be 10 char arrays of size 1024*sizeof(char) end-to-end ..one right after another in memory. So, it's going to be a total size of 10*1024*sizeof(char)


If you look at the example winsock code at http://msdn.microsoft.com/en-us/library/windows/desktop/ms740148%28v=vs.85%29.aspx it uses:

char SendBuf[1024];
int BufLen = 1024;

This could be imagined as:

char SendBuf[1][1024];
int BufLen = 1*1024;

Which might make it a little easier to see how size works with these kinds of arrays.