Link to home
Start Free TrialLog in
Avatar of jonn_g
jonn_g

asked on

reading bytes from memory tables in ANSI C

Well I have this void * bufPtr pointing somewhere in the memory and putting there a record from a .dat file.
I allocate memory for bufPtr with this: bufPtr=malloc(sizeof(recSize));

Now I want to find a way and read specific amount of bytes from bufPtr. For example read 4 bytes beggining from byte 60...And that is my problem!

Please help! Urgent!


jonn
Avatar of sunnycoder
sunnycoder
Flag of India image

Hi jonn_g,

((char *) bufPtr )  this will cast your void * to char *

((char *) bufPtr ) + 60  --- points to the desired location .... you can then read as may bytes as you want

for ( i=0; i<4; i++ )
    printf ( "%c", ((char *) bufPtr ) + 60 + i );

Sunnycoder
Hi jonn_g,
> Well I have this void * bufPtr pointing somewhere in the memory and
> putting there a record from a .dat file.
> I allocate memory for bufPtr with this: bufPtr=malloc(sizeof(recSize));

As you defined your record as a struct (recSize), you should also define bufPtr as
recSize* bufPtr;

Then you can simply access a field as defined in your record. Be aware that C applies padding to entries bigger than one byte. Check if you need a
#pragma pack
or something similar (it's compiler-specific) if your record offsets don't match your structure definition.

Cheers,
Stefan
Avatar of jonn_g
jonn_g

ASKER

I forgot to mention that in my first post that's why I'm doing this comment:

What I mostly need is not to read these bytes from the memory and print the fields, but to compare the fields with some given constant values. And that is the most difficult part...
Should I put the fields into specific variables and compare them or what...

For exaple I have the 4 bytes from the for ( i=0; i<4; i++ )
                                                           printf ( "%2f", ((char *) bufPtr ) + 60 + i );
and they represent a float number.

How should I compare them with a value i.e 7.5 when the only thing I shall use is a void *valPtr pointer to the constant value 7.5
Now that you know how to get that address, cast it to float * and compare

(float *) (((char *) bufPtr ) + 60 )

points to the float you want
Avatar of jonn_g

ASKER

Well I'm not the best when it comes to ANSI C programming...but i thought this was for sure.
Let's say we have the code

#include <stdio.h>

int test()
{
      return 1;

}

void main()
{
      int t;
      t=test();
      printf("t=%d\n", &t);
}

I expected that the output at the screen would be "t=1"....It's not it's something like 1245052 and doen't seem somehow connected to the value the test() functin returns...
The problem is how can I save the value that a function returns into a variable...In this specific case how can I save somewhere the value "1".

Sorry for continuing on this topic but it's urgent!
ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial