Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 22nd, 2006, 1:48 PM   #1
TaviO!
Newbie
 
Join Date: Feb 2006
Posts: 12
Rep Power: 0 TaviO! is on a distinguished road
HELP! "field has incomplete type"

Hey guys!
I've been having a problem while trying to insert a struct inside of a struct! The initial idea was to have a single struct with many int and char fields, but I decided to put those fields on a separate struct and insert this new struct on the first one. Here's how it ended:

typedef struct aviao
{
struct informacao info;
struct aviao *prox;
}Aviao;

typedef struct informacao
{
int combustivel;
int estado;
int tempovoo;
char compania[2];
char rota[3];
}Informacao;

Example:
If I declare Aviao a, b;
I can copy their properties just by doing a.info = b.info
I mean, it would be so if the compiler wouldn't give me the following error:
"field `info' has incomplete type " wich shows up on the 3rd line of the code
that I posted here.
I googled this error and it seems to be the same thing that happens when you declare an array without stating it's size.
But hell, I can't seem to relate this kind of error with anything that happens on my code
I would be glad if anyone could give me a hand at this one.
Cheers!

TaviO!
P.S.: Whatever variable names you don't understand is written in Portuguese =D
TaviO! is offline   Reply With Quote
Old Apr 22nd, 2006, 2:20 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
You'll kick yourself when you get this: declare and define before you use. With pointers, you don't have to define things before using them, but you still have to declare them.

typedef struct informacao
{
	int combustivel;
	int estado;
	int tempovoo;
	char compania[2];
	char rota[3];
}
Informacao;

typedef struct aviao
{
	struct informacao info;
	struct aviao *prox;
}
Aviao;
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Apr 22nd, 2006, 2:28 PM   #3
TaviO!
Newbie
 
Join Date: Feb 2006
Posts: 12
Rep Power: 0 TaviO! is on a distinguished road
LOL
It really worked!
Can't believe I made such a mistake!
Thanks man, you saved my brain from melting :banana:
TaviO! is offline   Reply With Quote
Old Apr 22nd, 2006, 10:20 PM   #4
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
For a compiled language, i find it stupid having to define things in order. If they add a pass to the C compiler to figure out the types in the program first and then carry on.

Would that kill them, or is it just too easy and straight forward. You gotta love it when programmers obfusticate their code. Or when they start using thoughtless abbreviations to complicate things and make it look like they know a thing or two. We have a guy like that in the shop, can't stand his stupid acronyms. He comes up with a new one every week...
OpenLoop is offline   Reply With Quote
Old Apr 22nd, 2006, 11:45 PM   #5
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by OpenLoop
For a compiled language, i find it stupid having to define things in order. If they add a pass to the C compiler to figure out the types in the program first and then carry on.
It wouldn't be done in a separate pass of the compiler. The way to do it would be for the compiler to maintain a list of things with unresolved types, and resolve the types as it scans through a source file.

The practical difficulty of this is that C is designed so that a C compiler is usable on a machine with severely limited available memory. The "put things first" was originally designed as an optimisation to reduce memory usage of the compiler, and there are still enough C compilers targetting computing hardware with limited resources to make it challenging pushing such a change through the standardisation process.

Quote:
Originally Posted by OpenLoop
Would that kill them, or is it just too easy and straight forward.
As I said, there is a practical issue for compilers targeting machines with limited memory resources. A large body of C code exists, and is continuing to be created, on such machines.

From a programming perspective, it would also introduce dependencies between parts of a source file which would make it more difficult to separate a program into a set of smaller but well contained source files.

Quote:
Originally Posted by OpenLoop
You gotta love it when programmers obfusticate their code. Or when they start using thoughtless abbreviations to complicate things and make it look like they know a thing or two. We have a guy like that in the shop, can't stand his stupid acronyms. He comes up with a new one every week...
What we're talking about has nothing to do with code obfuscation.

If you remember the rule "declare or define it appropriately BEFORE you use it" the sort of problem that started this thread are not that difficult to resolve.

As an aside, it is possible to achieve what you want in C++ using coding constructs that are VERY similar to Java. However, those coding constructs are viewed as bad practice by C++ programmers because they introduce a program architecture that limits ability to do some other things (which are, incidentally, not allowed in Java).
grumpy is offline   Reply With Quote
Old Apr 24th, 2006, 3:42 PM   #6
arctosh
Newbie
 
Join Date: Apr 2006
Posts: 1
Rep Power: 0 arctosh is on a distinguished road
help me!!! The question require me to modified this program to verify :
a) The validity of the each IP header checksum
b) The validity of each TCP Header checksum
c) Whether the IP checksum is correct or not.
d) If IP Checksum is incorrect, display the correct IP checksum for the packet
e) If TCP Checksum is incorrect, display the correct TCP checksum for the packet

This is the program :


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include "ethtype.h"
#include "eth.h"
#include "ip.h"
#include "arp.h"
#include "tcp.h"
#include "icmp.h"
#include "udp.h"

#define NULL 0
#define TCPDUMP_MAGIC 0xa1b2c3d4 /* Tcpdump Magic Number (Preamble) */
#define PCAP_VERSION_MAJOR 2 /* Tcpdump Version Major (Preamble) */
#define PCAP_VERSION_MINOR 4 /* Tcpdump Version Minor (Preamble) */

#define DLT_NULL 0 /* Data Link Type Null */
#define DLT_EN10MB 1 /* Data Link Type for Ethernet II 100 MB and above */
#define DLT_EN3MB 2 /* Data Link Type for 3 Mb Experimental Ethernet */
# padding

FILE *input;


typedef struct packet_header
{
unsigned int magic; /* Tcpdump Magic Number */
unsigned short version_major; /* Tcpdump Version Major */
unsigned short version_minor; /* Tcpdump Version Minor */
unsigned int thiszone; /* GMT to Local Correction */
unsigned int sigfigs; /* Accuracy of timestamps */
unsigned int snaplen; /* Max Length of Portion of Saved Packet */
unsigned int linktype; /* Data Link Type */
} hdr;

typedef struct packet_timestamp
{
unsigned int tv_sec; /* Timestamp in Seconds */
unsigned int tv_usec; /* Timestamp in Micro Seconds */
/* Total Length of Packet Portion (Ethernet Length until the End of Each Packet) */
unsigned int caplen;
unsigned int len; /* Length of the Packet (Off Wire) */
} tt;

typedef struct ether_header
{
unsigned char edst[ETHER_ADDR_LEN]; /* Ethernet Destination Address */
unsigned char esrc[ETHER_ADDR_LEN]; /* Ethernet Source Address */
unsigned short etype; /* Ethernet Protocol Type */
} eth;

typedef struct ip_header
{
unsigned char ip_vhl; /* IP Version Number and Header Length */
unsigned char ip_tos; /* IP Type of Service */
unsigned char ip_len[IP_TTL_LEN]; /* IP Total Length */
unsigned char ip_id[IP_IDENT_LEN]; /* IP Identification */
unsigned short ip_off; /* IP Offset */
unsigned char ip_ttl; /* IP Time To Live */
unsigned char ip_proto; /* IP Protocol Type */
unsigned char ip_sum[IP_SUM_LEN]; /* IP Checksum */
unsigned char ip_src[IP_ADDR_LEN]; /* IP Source Address */
unsigned char ip_dst[IP_ADDR_LEN]; /* IP Destination Address */
} ip;

unsigned short ip_len, ip_id, ip_sum;

typedef struct arp_header
{
unsigned char arp_htype[ARP_TYPE_LEN]; /* ARP Hardware Format */
unsigned char arp_ptype[ARP_TYPE_LEN]; /* ARP Protocol Format */
unsigned char arp_haddr; /* ARP Hardware Address Length */
unsigned char arp_paddr; /* ARP Protocol Address Length */
unsigned char arp_op[ARP_OPCODE_LEN]; /* ARP Opcode */
unsigned char arp_hsrc[ARP_ETHER_ADDR_LEN]; /* ARP Sender Ethernet Address */
unsigned char arp_psrc[ARP_PROTO_ADDR_LEN]; /* ARP Sender IP Address */
unsigned char arp_hdst[ARP_ETHER_ADDR_LEN]; /* ARP Target Ethernet Address */
unsigned char arp_pdst[ARP_PROTO_ADDR_LEN]; /* ARP Target IP Address */
} arp;

unsigned short arp_htype, arp_ptype, arp_op;

typedef struct icmp_header
{
unsigned char icmp_type; /* ICMP Type of Message */
unsigned char icmp_code; /* ICMP Type Sub Code */
unsigned char icmp_sum[ICMP_SUM_LEN]; /* ICMP Checksum */
} icmp;

unsigned short icmp_sum;

typedef struct tcp_header
{
unsigned char tcp_sport[TCP_PORT_LEN]; /* TCP Source Port */
unsigned char tcp_dport[TCP_PORT_LEN]; /* TCP Destination Port */
unsigned char tcp_seq[TCP_SEQ_LEN]; /* TCP Sequence Number */
unsigned char tcp_ack[TCP_ACK_LEN]; /* TCP Acknowledgement Number */
unsigned char tcp_len; /* TCP Data Offset */
unsigned char tcp_flags; /* TCP Flags */
unsigned char tcp_win[TCP_WIN_LEN]; /* TCP Window */
unsigned char tcp_sum[TCP_SUM_LEN]; /* TCP Checksum */
unsigned char tcp_ptr[TCP_PTR_LEN]; /* TCP Urgent Pointer */
} tcp;

unsigned short tcp_src, tcp_dst, tcp_seq, tcp_ack, tcp_win, tcp_sum, tcp_ptr;

typedef struct udp_header
{
unsigned char udp_sport[UDP_PORT_LEN]; /* UDP Source Port */
unsigned char udp_dport[UDP_PORT_LEN]; /* UDP Destination Port */
unsigned char udp_len[UDP_HDR_LEN]; /* UDP Length */
unsigned char udp_sum[UDP_SUM_LEN]; /* UDP Checksum */
} udp;

unsigned short udp_src, udp_dst, udp_len, udp_sum;

int main(int argc, char *argv[])
{
typedef unsigned short u16;
typedef unsigned long u32;

u16 word16;
u16 padd=0;

unsigned int remain_len = 0;
unsigned char temp=0, hlen, version, tlen;
int i, count=0;

struct packet_header hdr; /* Initialize Packet Header Structure */
struct packet_timestamp tt; /* Initialize Timestamp Structure */
struct ether_header eth; /* Initialize Ethernet Structure */
struct ip_header ip; /* Initialize IP Header Structure */
struct arp_header arp; /* Initialize ARP Header Structure */
struct icmp_header icmp; /* Initialize ICMP Header Structure */
struct tcp_header tcp; /* Initialize TCP Header Structure */
struct udp_header udp; /* Initialize UDP Header Structure */
unsigned char buff;

input = fopen("abc", "rb"); /* Open Input File */
if(fopen == NULL)
printf("Cannot open saved windump file.\n");
else
{
fread((char *) &hdr, sizeof(hdr), 1, input); /* Read & Display Packet Header Information */

printf("\n********** ********** PACKET HEADER ********** ***********\n");
printf("Preamble\n");
printf("Packet Header Length : %u bytes\n", sizeof(hdr));
printf("Magic Number : %u\n", hdr.magic);
printf("Version Major : %u\n", hdr.version_major);
printf("Version Minor : %u\n", hdr.version_minor);
printf("GMT to Local Correction : %u\n", hdr.thiszone);
printf("Jacked Packet with Length of : %u\n", hdr.snaplen);
printf("Accuracy to Timestamp : %u\n", hdr.sigfigs);
printf("Data Link Type (Ethernet Type II = 1) : %u\n", hdr.linktype);


/* Use While Loop to Set the Packet Boundary */
while(fread((char *) &tt, sizeof(tt), 1, input)) /* Read & Display Timestamp Information */
{
++count;
printf("\n");
printf("\n********** ********** TIMESTAMP & ETHERNET FRAME ********** ***********\n");

printf("\nPacket Number: %d", count); /* Display Packet Number */
printf("\nThe Packets are Captured in : %u Seconds\n", tt.tv_sec);
printf("The Packets are Captured in : %u Micro-seconds\n", tt.tv_usec);

/* Use caplen to Find the Remaining Data Segment */
printf("The Actual Packet Length: %u Bytes\n", tt.caplen);
printf("Packet Length (Off Wire): %u Bytes\n", tt.len);

fread((char *) &eth, sizeof(eth), 1, input); /* Read & display ethernet header information */
printf("Ethernet Header Length : %u bytes\n", sizeof(eth));
printf("MAC Destination Address : [hex] %x :%x :%x :%x :%x :%x \n\t\t\t [dec] %d :%d :%d :%d :%d :%d\n",
eth.edst[0], eth.edst[1],
eth.edst[2], eth.edst[3], eth.edst[4], eth.edst[5], eth.edst[0], eth.edst[1],
eth.edst[2], eth.edst[3], eth.edst[4], eth.edst[5], eth.edst[6]);

printf("MAC Source Address : [hex] %x :%x :%x :%x :%x :%x \n\t\t\t [dec] %d :%d :%d :%d :%d :%d\n",
eth.esrc[0], eth.esrc[1], eth.esrc[2],
eth.esrc[3], eth.esrc[4], eth.esrc[5], eth.esrc[0], eth.esrc[1],
eth.esrc[2], eth.esrc[3], eth.esrc[4], eth.esrc[5]);

for (i=0;i<tt.caplen -14;i++)
{ fread((char *) &buff, sizeof(buff), 1 , input);
printf(" %x", buff);
}

} // end while


} // end main else

fclose(input); // Close input file


return (0);
}
arctosh is offline   Reply With Quote
Old Apr 24th, 2006, 5:05 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Arctosh, read the forum's rules and FAQ and put your code in tags. It doesn't matter if you're stupid or inconsiderate, the effect is the same. Take two aspirin and surf to www.puerileprogrammers.com in the morning.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 1:48 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC