Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,076 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,584 people online right now. Registration is fast and FREE... Join Now!




Problem with array initialization

 
Reply to this topicStart new topic

Problem with array initialization

joeserhal
21 Apr, 2008 - 04:14 PM
Post #1

New D.I.C Head
*

Joined: 16 Apr, 2008
Posts: 15


My Contributions
Hi there,
I'm trying to initialize a two dimensional array (that hold structures) by making a certain field "on" equal to 0.

CODE
typedef struct packet_info {
        int on;
        u_char  src_ip;                 /* source ip address */
        u_short dst_port;               /* destination port address */
        u_char  flag;
        #define TH_FIN  0x01
        #define TH_SYN  0x02
        #define TH_RST  0x04
        #define TH_PUSH 0x08
        #define TH_ACK  0x10
        #define TH_URG  0x20
        #define TH_ECE  0x40
        #define TH_CWR  0x80
        #define TH_FLAGS        (TH_FIN|TH_SYN|TH_RST|TH_ACK|TH_URG|TH_ECE|TH_CWR)
}Packet_Info;


CODE
int main(int argc, char **argv)
{
        int i,j;
        char *dev = NULL;                       /* capture device name */
        char errbuf[PCAP_ERRBUF_SIZE];          /* error buffer */
        pcap_t *handle;                         /* packet capture handle */

        char filter_exp[] = "";         /* filter expression [3] */
        struct bpf_program fp;                  /* compiled filter program (expression) */
        bpf_u_int32 mask;                       /* subnet mask */
        bpf_u_int32 net;                        /* ip */
        int num_packets = 20;                   /* number of packets to capture */

        Packet_Info array[10][10];

        for(i=0;i<10;i++)
          for(j=0;j<10;j++)
            (array[j][i])->on = 0;

        for(i=0;i<10;i++)
          for(j=0;j<10;j++){
            printf("source %d packet %d ip field is %d\n", i+1, j+1,array[j][i]->on);
            getchar();
        }
....


However i'm getting this error:
invalid type argument of '->'

Any ideas?

Also, how can I initialize the values of "src_ip", and "dst_port" to a certain value as I am not very familiar with the types: 'uchar' and 'u_short' ??


Thanks


User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Problem With Array Initialization
21 Apr, 2008 - 04:30 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



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

My Contributions
QUOTE(joeserhal @ 21 Apr, 2008 - 05:14 PM) *

Hi there,
I'm trying to initialize a two dimensional array (that hold structures) by making a certain field "on" equal to 0.

....

However i'm getting this error:
invalid type argument of '->'

Any ideas?

Also, how can I initialize the values of "src_ip", and "dst_port" to a certain value as I am not very familiar with the types: 'uchar' and 'u_short' ??


Thanks


Yes. Packet_Info is not a pointer type, and thus, one needs to use a ., not the -> indirection convenience syntax.

So it would be array[j][i].on = 0;

Oh, and about the other two... I would bet [monopoly money] that u_char stands for unsigned char, though I'm not sure how that would work here, as an IP address will likely need at least four bytes, or 7-15 if it's in the form of a narrow character string (dotted notation).

This post has been edited by perfectly.insane: 21 Apr, 2008 - 04:34 PM
User is offlineProfile CardPM
+Quote Post

joeserhal
RE: Problem With Array Initialization
21 Apr, 2008 - 04:39 PM
Post #3

New D.I.C Head
*

Joined: 16 Apr, 2008
Posts: 15


My Contributions
Thanks a lot for the help!!!

Um, do you know how i can do the same for the variables "src_ip" and "dst_port" found in the structure Packet_Info?? I mean these are of type u_char & u_short, so how can I initialize them to a null value let's say?!!
User is offlineProfile CardPM
+Quote Post

perfectly.insane
RE: Problem With Array Initialization
21 Apr, 2008 - 04:45 PM
Post #4

D.I.C Addict
Group Icon

Joined: 22 Mar, 2008
Posts: 558



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

My Contributions
QUOTE(joeserhal @ 21 Apr, 2008 - 05:39 PM) *

Thanks a lot for the help!!!

Um, do you know how i can do the same for the variables "src_ip" and "dst_port" found in the structure Packet_Info?? I mean these are of type u_char & u_short, so how can I initialize them to a null value let's say?!!


Well, these variables can also be initialized by setting the variable to a zero (in the same way as the on variable). I'm questioning though the use of u_char for the ip address, as you won't be able to store an IP address in that type. u_char represents a single character.


For future reference:
A quick way of zeroing a structure as declared (since each array element is stored contiguously):

std::memset(&array, 0, sizeof(array));

(one must also include cstdlib).

This post has been edited by perfectly.insane: 21 Apr, 2008 - 04:49 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 07:25PM

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