Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,175 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,375 people online right now. Registration is fast and FREE... Join Now!




Struct into an Array

 
Reply to this topicStart new topic

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

eudos
31 May, 2008 - 10:08 PM
Post #1

D.I.C Head
**

Joined: 23 Apr, 2006
Posts: 58


My Contributions
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
CODE


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: 1 Jun, 2008 - 01:51 AM
User is offlineProfile CardPM
+Quote Post

KYA
RE: Struct Into An Array
1 Jun, 2008 - 03:06 AM
Post #2

#include <nerd.h>
Group Icon

Joined: 14 Sep, 2007
Posts: 5,045



Thanked: 124 times
Dream Kudos: 1200
My Contributions
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: 1 Jun, 2008 - 03:08 AM
User is online!Profile CardPM
+Quote Post

baavgai
RE: Struct Into An Array
1 Jun, 2008 - 04:07 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,046



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
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:
cpp

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: 1 Jun, 2008 - 04:07 AM
User is online!Profile CardPM
+Quote Post

eudos
RE: Struct Into An Array
1 Jun, 2008 - 04:28 AM
Post #4

D.I.C Head
**

Joined: 23 Apr, 2006
Posts: 58


My Contributions
QUOTE(baavgai @ 1 Jun, 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:
cpp

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
CODE

#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

CODE

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



so Hope i wrote my problem i an understandable way

thank you smile.gif



User is offlineProfile CardPM
+Quote Post

baavgai
RE: Struct Into An Array
1 Jun, 2008 - 06:26 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,046



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Didn't I say that you'll have an error treating it as a string, because of the zeros? tongue.gif

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.

User is online!Profile CardPM
+Quote Post

perfectly.insane
RE: Struct Into An Array
1 Jun, 2008 - 07:36 AM
Post #6

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



Thanked: 46 times
Dream Kudos: 25
Expert In: C/C++

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

eudos
RE: Struct Into An Array
1 Jun, 2008 - 07:37 AM
Post #7

D.I.C Head
**

Joined: 23 Apr, 2006
Posts: 58


My Contributions
Works fine now...thanks a lot
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Struct Into An Array
1 Jun, 2008 - 08:48 AM
Post #8

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,046



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
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:
CODE

RegMsg_t regMsg;
unsigned char * outMsgPtr = (unsigned char *)&regMsg;


And either treat your struct as a struct or an array of bytes.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 10:43AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month