By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,346 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

Passing structure value to pointer

P: n/a
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;
}
Nov 13 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

Nov 13 '05 #2

P: n/a
> 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
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.