Passing structure value to pointer

Discussion in 'C Programming' started by Andrew, Nov 23, 2003.

  1. Andrew

    Andrew Guest

    This is a homework question that has gone passed it's due date,
    however I still want to figure it out. This program initializes three
    structures, writes them to a binary file, reads them back out and is
    supposed to reverse the Endian. I cannot get the
    ReverseEndian(&TempStore->flt, sizeof(TempStore->flt));
    function call to pass the pointer to
    void ReverseEndian(void *ptr, size_t size)
    I did have it pass a couple of times so I am suspicious of the
    compiler(.NET), but I am a newbie so I need someone to throw some
    experience at this to make sure I didn't over look the obvious.
    Thanks for any help you can offer.

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

    #define NumOfWriteObjects 1
    #define BUFFSIZE 256
    #define FileSize 20

    void ReverseEndian(void *ptr, size_t size);

    struct Test
    {
    float flt;
    double dbl;
    void *vptr;
    }Test;

    int main(void)
    { //Declare and initialize array
    struct Test TestArray[3] = {{12.3F, 6666L, "old"}, {45.6F, 7777L,
    "houses"}, {78.9F, 8888L, "always"}};

    int count = sizeof(TestArray)/sizeof(TestArray[0]);
    FILE *flptr;
    char BinaryFile[FileSize];
    struct Test TempStore[BUFFSIZE];

    printf("Please enter a binary storage file in XXXXXX.bin format: ");
    scanf("%s", &BinaryFile);

    if((flptr = fopen(BinaryFile, "wb")) == NULL)
    {
    printf("Error opening write file\n\n");
    return(EXIT_FAILURE);
    }

    if(count != fwrite(TestArray, sizeof(TestArray[0]), count, flptr))
    {
    printf("Error storing Array\n\n");
    return(EXIT_FAILURE);
    }

    fclose(flptr);

    if((flptr = fopen(BinaryFile, "rb")) == NULL)
    {
    printf("Error opening read file\n\n");
    return(EXIT_FAILURE);
    }

    while(fread(TempStore, sizeof(Test), 1, flptr))
    {

    ReverseEndian(&TempStore->flt, sizeof(TempStore->flt));
    ReverseEndian(&TempStore->dbl, sizeof(TempStore->dbl));
    ReverseEndian(&TempStore->vptr, sizeof(TempStore->vptr));

    }

    fclose(flptr);

    return(EXIT_SUCCESS);
    }

    void ReverseEndian(void *ptr, size_t size)
    {
    char temp = '0';
    char TempStr[BUFFSIZE] = {(char )ptr};
    unsigned int x, y;

    for(x = 0, y = size - 1; x < (size / 2); ++x, --y)
    {
    temp = TempStr[y];
    TempStr[y] = TempStr[x];
    TempStr[x] = temp;
    }

    ptr = (void *)TempStr;
    }
     
    Andrew, Nov 23, 2003
    #1
    1. Advertising

  2. Andrew

    Artie Gold Guest

    Andrew wrote:
    > This is a homework question that has gone passed it's due date,
    > however I still want to figure it out. This program initializes three
    > structures, writes them to a binary file, reads them back out and is
    > supposed to reverse the Endian. I cannot get the
    > ReverseEndian(&TempStore->flt, sizeof(TempStore->flt));
    > function call to pass the pointer to
    > void ReverseEndian(void *ptr, size_t size)
    > I did have it pass a couple of times so I am suspicious of the
    > compiler(.NET), but I am a newbie so I need someone to throw some
    > experience at this to make sure I didn't over look the obvious.
    > Thanks for any help you can offer.


    OK. Fair enough.

    You were *real* close; please see below.
    [snip]


    >
    > void ReverseEndian(void *ptr, size_t size)
    > {
    > char temp = '0';
    > char TempStr[BUFFSIZE] = {(char )ptr};


    Consider the above definition. What you have written says: "I want
    an array of `char's of length BUFFSIZE, whose first element is the
    value of the pointer-to-void `ptr' cast to `char'." Erm, not what
    you want. At all.

    What you want (as a first approximation, see below) is the much simpler:

    char *TempStr = ptr;

    > unsigned int x, y;
    >
    > for(x = 0, y = size - 1; x < (size / 2); ++x, --y)
    > {
    > temp = TempStr[y];
    > TempStr[y] = TempStr[x];
    > TempStr[x] = temp;
    > }
    >
    > ptr = (void *)TempStr;


    The cast is unnecessary.

    > }


    Unfortunately, you've merely written a routine that reverses the
    bytes in a buffer -- and it *will* work if you happen to be dealing
    with types whose size is the same as the native word size on your
    machine; unfortunately this is not likely[1] to be the case for each
    of int, float and double.

    You'll need to do it on a `word by word' basis.

    HTH,
    --ag




    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.
     
    Artie Gold, Nov 23, 2003
    #2
    1. Advertising

  3. Andrew

    Andrew Guest

    > char *TempStr = ptr;
    > HTH,
    > --ag


    That did it!!!! Quite likely I do not have the Debugger setup
    correctly to show the pointer passing. I get to sleep tonight.
    Thanks
     
    Andrew, Nov 24, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jimjim
    Replies:
    16
    Views:
    844
    Jordan Abel
    Mar 28, 2006
  2. Replies:
    6
    Views:
    372
    Jack Klein
    Aug 27, 2006
  3. iskeletor

    problem in passing a value into a structure

    iskeletor, Dec 14, 2006, in forum: C Programming
    Replies:
    3
    Views:
    336
    Barry Schwarz
    Dec 15, 2006
  4. pereges
    Replies:
    7
    Views:
    440
    pereges
    Jun 1, 2008
  5. A
    Replies:
    27
    Views:
    1,606
    Jorgen Grahn
    Apr 17, 2011
Loading...

Share This Page