Struct into an Array

How to read a whole Struct into an Array(UDP socket)

Page 1 of 1

7 Replies - 2806 Views - Last Post: 01 June 2008 - 09:48 AM Rate Topic: -----

#1 eudos  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 23-April 06

Struct into an Array

Posted 31 May 2008 - 11:08 PM

Hello,

I am trying to write a UDP socket client program. and it seems the networking part is working.
The problem am having is of the way i represent data.

here is the registration message Struct

typedef struct {
	char passwd[16];
	char username[14];
	unsigned short tcpPort;
} RegMsg_t;



main()

RegMsg_t *regMsg				   //a pointer to the struct

regMsg->psswd = "joy";		   //trying to read in the psswd array
						 // OR	 
strcpy(regMsg->psswd,"joy");	 //a different way of reading in to the array


char outMsg[N];				  //Array of the out message( the whole struct)

regMsg= (RegMsg_t *)outMsg;	 //trying to equate the pointer regMsg and the outMsg array






In the code above Non seems working. those are the two ways m trying to read data in to the struct (THE MAIN QUESION IS HOW TO CHANGE THE STRUCT INTO AN ARRAY

Then the other part is to change this whole struct into an ARRAY for the sento function of the UDP socket


these are the problems holding me up. Can anybody pls help me..
Thnks

This post has been edited by eudos: 01 June 2008 - 02:51 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Struct into an Array

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3123
  • View blog
  • Posts: 19,167
  • Joined: 14-September 07

Re: Struct into an Array

Posted 01 June 2008 - 04:06 AM

Well if the above code isn't working, it is because in your struct the array is passwd, but in implementation you are calling it as psswd.

Does it have to be only one array? A struct or even a class seems to be the most logical way to represent the data you are currently dealing with.

However, you can make it all one array, the first 16 elements (0-15) can take a variable, the next 14 elements (16-29) takes the user name and the last slots (30-however big the port # is) takes the int for the tcp port.

This post has been edited by KYA: 01 June 2008 - 04:08 AM

Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: Struct into an Array

Posted 01 June 2008 - 05:07 AM

Looks like you have a 32 byte structure. If you just point to it with a char pointer, you now have something that appears to be an array of bytes. Unfortunately, C uses the value of 0 to terminate string, to a string method probably wont do what you want.

I believe this code will do what you're looking for:
RegMsg_t regMsg;
size_t size = sizeof(regMsg);
char outMsg[size];
    
strcpy(regMsg.username,"user");
strcpy(regMsg.passwd,"pass");
memcpy(outMsg, &regMsg, size);


This post has been edited by baavgai: 01 June 2008 - 05:07 AM

Was This Post Helpful? 0
  • +
  • -

#4 eudos  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 23-April 06

Re: Struct into an Array

Posted 01 June 2008 - 05:28 AM

View Postbaavgai, on 1 Jun, 2008 - 05:07 AM, said:

Looks like you have a 32 byte structure. If you just point to it with a char pointer, you now have something that appears to be an array of bytes. Unfortunately, C uses the value of 0 to terminate string, to a string method probably wont do what you want.

I believe this code will do what you're looking for:
RegMsg_t regMsg;
size_t size = sizeof(regMsg);
char outMsg[size];
    
strcpy(regMsg.username,"user");
strcpy(regMsg.passwd,"pass");
memcpy(outMsg, &regMsg, size);




thank you for the reply. i have used ur idea and still got a problem. here is the code below
#include <stdio.h>
#include <string.h>
#define MAX_MSG_LEN  1200

typedef struct {
	char passwd[16];
	char username[14];
	unsigned short tcpPort;
} RegMsg_t;


int main (int argv, char **argc){


	  RegMsg_t regMsg;
	  size_t size = sizeof(regMsg);
	  char outMsg[size];

	  strcpy(regMsg.username,"user");
	  strcpy(regMsg.passwd,"pass");
	  memcpy(outMsg, &regMsg, size);
	  
	  regMsg.tcpPort  =  55666;



	  printf("Out Message Arrray  is  %s\n",  outMsg);

	  //the problem here is ..it only prints "pass" (password) only
	  //that means m not putting all the other( username and tcp port) the array

return 0;
}





in the above code m just testing whether the outMsg got all the stuff needed by the server side.
Below is the function m using to send to the server side as a UDP socket

e = sendto(sock, outMsg, outMsgLen, 0,
			(struct sockaddr *)&toAddr, sizeof(toAddr));




so Hope i wrote my problem i an understandable way

thank you :)
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: Struct into an Array

Posted 01 June 2008 - 07:26 AM

Didn't I say that you'll have an error treating it as a string, because of the zeros? :P

Try this: for(i=0; i<size; i++) { printf("%c:", outMsg[i]); } printf("\n");
Or this: for(i=0; i<size; i++) { printf("%d:", outMsg[i]); } printf("\n");

You'll probably get negative numbers on the second one. If that bugs you, make char outMsg[size]; into unsigned char outMsg[size];

That last unsigned short tcpPort; value will be there, but you might need to do a little mental calculation to figure it out. Just keep in mind, it's all just bytes. Everything else is just a form that makes it easier for us humans to deal with.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#6 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Struct into an Array

Posted 01 June 2008 - 08:36 AM

You could also write your packet to a file, so that you can look at it with a hex editor (as which I assume you're printing the packet for debugging purposes).

FILE* f = fopen("somefile", "wb");
fwrite(outMsg, 1, sizeof(outMsg), f);
fclose(f);

Or you could also use Wireshark to look at it.
Was This Post Helpful? 0
  • +
  • -

#7 eudos  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 23-April 06

Re: Struct into an Array

Posted 01 June 2008 - 08:37 AM

Works fine now...thanks a lot
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,857
  • Joined: 16-October 07

Re: Struct into an Array

Posted 01 June 2008 - 09:48 AM

Glad to help.

I should probably note that the memcpy from one memory block into another is mostly a formality. You can treat your data as a different type in place; C doesn't really care.

So, you could do this instead:
RegMsg_t regMsg;
unsigned char * outMsgPtr = (unsigned char *)&regMsg;



And either treat your struct as a struct or an array of bytes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1