Passing structure value to pointer

A

Andrew

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;
}
 
A

Artie Gold

Andrew said:
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
 
A

Andrew

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top