0

I'm trying to send an integer array of size 480X640 to another machine over UDP using winsock programming in C++. I can typecast the array to char * before sending, but i can't figure out how to cast the char * back to integer array. The code segment looks like this:

int arrayData[480][640];    
char * arrayChar;

arrayChar = (char *)&arrayData;

// send it to the other end over UDP

// at the other end, receive and convert char * to integer array

Is this typecast valid? If anyone could help me with converting 'arrayChar' back to integer array, it would be great.

Vigo
  • 707
  • 4
  • 11
  • 29

2 Answers2

0

you can use reinterpret_cast and then cast it back at receiver side using same.

Abhishek Bansal
  • 5,197
  • 4
  • 40
  • 69
0

Answers.

  1. You don't need the & and it's better to use the right kind of cast in C++.

    char* ptr = static_cast<char*>(arrayData);

  2. For the cast you need see (How to cast simple pointer to a multidimensional-array of fixed size?)

  3. You've got over 1MB of data there, which is a lot of packets. Are you really sure you want to use UDP, which does not guarantee packets arrive in order (or at all)?

Community
  • 1
  • 1
david.pfx
  • 10,520
  • 3
  • 30
  • 63
  • This code segment deals with a real time communication..hence UDP is preferred..but i take your point concerning packet losses..will try TCP too! thanks.. – Vigo Mar 22 '14 at 14:41