473,396 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Passing structure value to pointer

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
2 5271
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
1
by: Tobias | last post by:
Hi! I have a problem which is quite tricky. I need to pass a struct from .NET to a native Win32 DLL. But i just need to pass the pointer to a reference of that struct. With my first struct this...
9
by: Just Me | last post by:
PARAFORMAT2 is a structure that SendMessage will return stuff in. Is the "ref" correct or since only a pointer is being passed should it be by value? Suppose I was passing data rather then...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
7
by: Jake Thompson | last post by:
Hello I created a DLL that has a function that is called from my main c program. In my exe I first get a a pointer to the address of the function by using GetProcAddress and on the dll side I...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
13
by: frakie | last post by:
Hi 'body, I'm experiencing difficulties on parameter passing. I wrote a library in last months, and now it crashes few times in a month because of errors in parameter passing: using gdb on the...
7
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.