Struct and array

This is a discussion on Struct and array within the C Programming forums, part of the General Programming Boards category; Hi all, i have a tyedef struct as following. Code: typedef union __Checksum { unsigned short sh; unsigned char ch[2]; ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    21

    Wink Struct and array

    Hi all,

    i have a tyedef struct as following.

    Code:
    typedef union __Checksum
    {
    	unsigned short sh;
    	unsigned char ch[2];
    } Chksum;
    
    typedef struct __TXPacket
    {
        unsigned char StartData;				
        unsigned char DestinationID;			    
        unsigned char SourceID;			
        unsigned char TotalLength;				
        unsigned char Command;			
        unsigned char Parameter;			
        Chksum Checksum;
    } TXPacket;
    
    since it is a typedef, can i do as follow?
    
    TXPacket PacketSend [8];
    
    PacketSend.StartData = 'M';
    PacketSend.DestinationID = 'A';
    .
    .
    .
    .
    .
    for (i = 0;i<8;i++)
           PacketSend[i] ;
          //do other things with the data as a normal array

    Is PacketSend.StartData = PacketSend [0]?


    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This would be the correct method for accessing the first StartData:
    Code:
    foo = PacketSend[0].StartData;
    ...
    PacketSend[0].StartData = foo;
    This doesn't actually do anything:
    Code:
    for (i = 0;i<8;i++)
           PacketSend[i] ;
    It's like saying:
    Code:
    for(i=0;i<8;i++)
        5;
    No effect.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    21

    Unhappy

    hi quzah

    Code:
    for (i = 0;i<8;i++)
           PacketSend[i] ;
          //do other things with the data as a normal array
    this part is not important. What i try to ask is whether

    PacketSend.StartData == PacketSend [0]?

    u said that
    foo = PacketSend[0].StartData;
    does this means that by doing the following

    Code:
    TXPacket PacketSend [8];
    i am creating 8 set of PacketSend instead of treating PacketSend as a array of 8 elements where the 8 elements are the elements of TXPacket struct ? Please clarify it to me as i a little bit confuse.

    Thanks

  4. #4
    Banned
    Join Date
    Jun 2005
    Posts
    594
    you are creating 8 of everything in the struct,

    Code:
    struct mystruct
    {
         int num;
         char letter;
    }TX[8];
    
    means i can do the following
    
    
    TX[0].num = 1;
    TX[1].num = 2;
    TX[2].num = 3;
    TX[3].num = 4;
    TX[4].num = 5;
    TX[5].num = 6;
    TX[6].num = 7;
    TX[7].num = 8;
    TX[0].letter = 'a'
    TX[1].letter = 'b'
    TX[2].letter = 'c'
    TX[3].letter = 'd'
    TX[4].letter = 'e'
    TX[5].letter = 'f'
    TX[6].letter = 'g'
    TX[7].letter = 'h'
    
    
    
    cout << TX[0].num << "\t" << TX[0].letter << endl;

  5. #5
    Banned
    Join Date
    Jun 2005
    Posts
    594
    here i typed up a code that you can actually compile

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct person
    {
    	int age;
    	char fletter;
    
    }people[5];
    
    int main()
    {
    	for(int i = 0; i != 5; i++)
    	{
    		people[i].age = i;
    		people[i].fletter = char(i+65);
    	}
    	for(int x = 0; x != 5; x++)
    	{
    		cout << people[x].age << "\t" << people[x].fletter << endl;
    	}
    	cin.get();
    	return 0;
    }

  6. #6
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by suzanne_lim
    this part is not important. What i try to ask is whether

    PacketSend.StartData == PacketSend [0]?
    No. PacketSend is an array of struct, not a struct, so PacketSend.StartData doesn't make sense, and your compiler will produce a diagnostic.

    On the other hand, PacketSend[0].Startdata is equivalent to *(PacketSend+0).Startdata is equivalent to *(PacketSend).Startdata is equivalent to PacketSend->Startdata. However, in this instance, using PacketSend->Startdata may be a little confusing, since it might lead the reader unfamiliar with the rest of your code to think that PacketSend is a pointer, rather than an array of 8 things.
    Quote Originally Posted by suzanne_lim
    Code:
    TXPacket PacketSend [8];
    i am creating 8 set of PacketSend instead of treating PacketSend as a array of 8 elements where the 8 elements are the elements of TXPacket struct ? Please clarify it to me as i a little bit confuse.
    You are creating an array of 8 structs. Where's the confusion?

    P.S. ILoveVectors, this is a C forum, would you not confuse the issue by posting C++ code?

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    21
    ooo....ic ic....Now i understand.Thanks everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21