0

I have a 2 dimensional array of structures, like so:

struct foo {
    int bar;
    float baz;
};

I'm actually learning about OS development right now, and one thing I'm trying to do is created a 2d array of these structures with the dimensions 80x25:

struct foo foobar[80][25];

Though I need to set it to the address 0xb8000, since this is where the video memory starts. Is there any way I can specify the address in which my array starts?

So far I have tried doing this:

struct foo foobar[80][25];
*foobar = (struct foo) 0xb8000;

But this doesn't work. Edit: also, is doing something like this legal and/or possible to the c99 standard?

mosmo
  • 411
  • 1
  • 4
  • 10

1 Answers1

1

You cannot declare array at a specific location, but you can use a pointer:

struct foo *x;
x = (struct foo*)0xb8000;

This should work, but probably the operating system will complain if you tried that from a normal program, unless it is DOS or something like that.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
Cecilio Pardo
  • 1,717
  • 10
  • 13
  • Bear in mind that OP wants to access the variable as a two dimensional array. Does your solution allow that? – kaylum Jan 04 '16 at 23:42
  • Hmm, okay. How would I go around doing the equivalent of x[10][15] with the pointer? `x[10 + 15 * buffer_width];`? – mosmo Jan 04 '16 at 23:42
  • The array thing cannot be done at all, unless your compiler/linker provides a nonstandard way. – Cecilio Pardo Jan 04 '16 at 23:43
  • Okay, thanks :) Also to clarify, where I put 10 + 15, which one is the row and which one is the column, and which of those are x,y? I always get confused about that. – mosmo Jan 04 '16 at 23:48
  • @CecilioPardo That's not quite true. Have a look at this for example: [Create a pointer to two-dimensional array](https://stackoverflow.com/questions/1052818/create-a-pointer-to-two-dimensional-array) – kaylum Jan 04 '16 at 23:50
  • The number with the * is the row (Y). The other one is the column (X). – Cecilio Pardo Jan 04 '16 at 23:52
  • @kaylum What's not true, and why? – Cecilio Pardo Jan 04 '16 at 23:54
  • @CecilioPardo "The array thing cannot be done at all". I thought you mean that it is not possible to declare a pointer to a multi dimensional array such that the pointer can then be dereferenced using multiple array indexes. That is possible and would fit more naturally for what the OP wants to do rather than flattening the array to one dimension. – kaylum Jan 04 '16 at 23:59
  • @kaylum No, I meant you cannot declare an array and tell the compiler to put it at a precise location in memory. Sorry for the mixup. – Cecilio Pardo Jan 05 '16 at 00:01