Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Structures and buffers

Status
Not open for further replies.

jerehart

Programmer
Jun 10, 1999
61
US
I am wondering if there is a cleaner way to place strctured data into a buffer. Mainly since the buffer is an address on a cPCI card and just wants the data.

Code:
typedef struct header{
   int h;
   int t ;
} HEAD;

typedef struct secondary{
   int ms;
   int us;
} SECOND;

void output ( unsigned int * data, int bytes)
{

     HEAD newHead;
     HEAD * buffer1;
     SECOND newSecond;
     SECOND * buffer2;
     unsigned int * buffer; 
     int i;

     newHead.h = 1;
     newHead.t = 55;

     newSecond.ms = 102;
     newSecond.us = 9;

     buffer1 = (HEAD *)(bar0 + 0x900);
     buffer2 = (SECOND *)(bar0 + 0x900 + sizeof(newHead));
     buffer = (unsigned int *)(bar0 + 0x900 + sizeof(newhead) + sizeof(newSecond));

     *buffer1 = newHead;
     *buffer2 = newSecond;
    
      for (i = 0; i < bytes; i++)
      {
           *buffer++ = *data++;
      }

}

This just looks sloppy to me any better way of doing this?
 
Try this for starters. This uses something called 'the chumminess of C'. You can declare an array of 1 but use more than one element as long as it is the last one declared in your structure.

Something strange about the coding: is the parameter meant to be bytes or words. You are copying 'bytes' unsigned ints. Should this figure be divided by sizeof (unsigned int)?
Code:
static const HEAD newHead = {1,55};
static const SECOND newSecond = {102,9};
typedef struct
{
   HEAD mHead;
   SECOND mSecond;
   unsigned int mData[1];
} CARD;
void output ( unsigned int * data, int bytes)
{
     CARD* card;
     unsigned int * buffer; 
     int i;

     card = (CARD*)(bar0 + 0x900);

     card->mHead = newHead;
     card->mSecond = newSecond;
     buffer = card->mData;
   
     for (i = 0; i < bytes; i++)
     {
        *buffer++ = *data++;
     }
}
 
Well there were some simplicities in the code snippet.
I call them bytes but yes I know they are words lazy names.
Making the items constants would be not be useful as they are updated by functions as GVs elsewhere. the array is an idea though but why not just a pointer?
 
If you have it as a pointer, it would not be contiguous data but a pointer to some other place.

Re assignments, you could also code it as
Code:
card->mHead->h = 1;
card->mHead->t = 55;
Saves an assignment to something else and then reassigning. Note that pointers and arrays aren't the same thing (not on every machine anyway)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top