Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

reading bytes from memory tables in ANSI C

Posted on 2004-04-16
8
Medium Priority
?
265 Views
Last Modified: 2010-04-17
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
0
Comment
Question by:jonn_g
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10842701
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
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10843009
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
0
 

Author Comment

by:jonn_g
ID: 10843150
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 10843217
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
0
 

Author Comment

by:jonn_g
ID: 10849699
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!
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 1000 total points
ID: 10856502
printf takes the variable as argument while you are passing the address of the variable ...

May be you confused it with scanf which takes address of a variable as argument

change printf("t=%d\n", &t); to printf("t=%d\n", t); and it should work fine
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Simple Linear Regression

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question