I am yet another college student trying to figure out malloc and pointers. I've spent the last 7 hours working on a rather involved project, and I'm stuck, stuck, stuck. After some fruitless googling, and forum searching, I've got a few questions that I'm hoping some of you would be willing to answer. Thanks in advance!
What I'm trying to do:
I need to create a "void pointer to the global memory" and allow the user to input certain commands that put data into various locations. For example: "stoint 2a3fh @4ah" stores the hexadecimal integer 2a3f in the hexadecimal address 4a. Possible data that will be put in are hexadecimal integers, and variable length strings. I also need a function that dumps the contents of the pointer in such a way that the hex value of each byte is displayed.
Questions:
- Void pointers?
my project saysI thought I could do that withOriginally Posted by my evil professorbut apparently if I put that above my main functions (where I declare all the function prototypes and whatnot) it doesn't compile.Code:void *mem[256] = (void *) malloc(256);
- How do I access the data?
I'm guessing that actually initializing the mem pointer is really easy to do. I'm sure that I'm close but I can't quite grasp it. What is completely baffling me though, is once I have this pointer of 256 bytes... how do I traverse it and print the data inside it? More importantly, how do I put other things inside the pointer (characters or integers)?
- What's %p?
In a small c program my professor wrote, he uses %p to show some data. I can't figure out what that's supposed to mean. I thought you used %x for hex? Incidentally, I've pored over that program for hints on what to do here... still rather confused though.
Ugh. That's a lot of stuff. If anyone is able to give me some ideas, I would really appreciate it. If I used terminology that doesn't make sense, sorry about that, I'm new to C (as you can tell). Also, if it clarifies things any, here's a sample memory dump given in the project (left column is relative memory location, middle column is hexadecimal output?, right column is any printable character):
Code:stoint ch @12h // user input stoint 3h @16h // user input stostr "this is fun" @22h // user input dump // user input 0000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0010: 00 00 0c 00 00 00 03 00 00 00 00 00 00 00 00 00 ................ 0020: 00 00 74 68 69 73 20 69 73 20 66 75 6e 00 00 00 ..this is fun... // LIES 0030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 0090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ 00f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................