1

My code:

.h file:

extern const int* g_position;

inline DWORD pos_x(BYTE v) {
    if (1 <= v && v <= 3)
        return g_position[v][0];

    return 0;
}

inline DWORD pos_y(BYTE v) {
    if (1 <= v && v <= 3)
        return g_position[v][1];

    return 0;
}

.cpp file:

const int* g_position = (int*) 0x86b2fdc;

What I am trying to do is address the function which is basically a two-dimensional array:

(DWORD (*)[4][2]) 0x86b2fdc<g_position>

For some reason I am getting the following error(s) while compiling:

In function 'gev::DWORD pos_y(BYTE)':
error: invalid types 'const int[int]' for array subscript
    return g_position[v][0];

What is wrong in my code? I am declaring the g_position as an array type so it should work.

1
  • g_position is only int *, so you can't dereference twice. Also, what exactly are you trying to do? In general, hard-coding a pointer is a bad idea. In any case the extern const ... declaration of g_position needs to match whatever declares that object at 0x86b2fdc Commented Aug 6, 2014 at 2:01

2 Answers 2

1

g_position is declared as int*, which allows only one level of dereferencing, but you are trying to perform two dereferences instead. You need to change the declaration to int** instead:

extern const int** g_position;

const int** g_position = (int**) 0x86b2fdc;

Update:

An int** is not quite the same thing as an int[][]. However, if you take into account that an int[4][2] has the same memory layout as an int[8], and you can access the content of an int[] using an int*, then try this instead:

extern const int* g_position;

inline DWORD pos_x(BYTE v)
{
    if (1 <= v && v <= 3)
        return g_position[(v*4)+0];

    return 0;
}

inline DWORD pos_y(BYTE v)
{
    if (1 <= v && v <= 3)
        return g_position[(v*4)+1];

    return 0;
}

const int* g_position = (int*) 0x86b2fdc;
Sign up to request clarification or add additional context in comments.

5 Comments

Getting this error: invalid conversion from 'int**' to 'const int**' [-fpermissive]. When I change the (int**) to the (const int**) my application is crashing.
Here is a backtrace snippet: #0 0x286b54c6 in gev::pos_y (e=3 '\003') at src/pos.h:14
If it is crashing, then the data at the memory address likely does not represent a true 2-dimensional array to begin with.
it does, this is the data at the memory address: int g_position[4][2] = { { 0, 0}, { 3461, 8480}, { 4115, 2340}, { 7875, 7575} };
"An int** is not quite the same thing as an int[][]" It's not in any way even close to being the same thing.
0

Is 0x86b2fdc the address of an array of arrays? Or the address of an array of pointers to arrays? Or is it the address of a pointer to something?

Assuming it is the address of an array of arrays of 4 ints, you can just declare g_position as a pointer to such:

extern const int (*g_position)[4];  /* pointer to arrays of 4 ints */

in the .cpp file:

const int (*g_position)[4] = (int (*)[4])0x86b2fdc;

Your basic confusion seems to be confusing pointers and arrays, which are not the same thing:

  • an array is a sequence of objects laid out one after the other in memory. You can't do anything with an array other than initialize it, get its size, and get a pointer to it or its 0th element

  • a pointer is an address that refers to memory. The memory pointed at may contain a single object of the appropriate type, or it may contain an array (in which case the pointer points at the 0th element of the array.

Whenever you refer to an array (except when using sizeof or unary &), the compiler silently gives you a pointer to the 0th element of the array, so the name of such a pointer is mostly interchangeable with the name of the array. Your pos_ functions don't use sizeof or & on g_position, so for your purposes, they are interchangeable. you just need to get the type correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.