1

There is a array of structures.

    static field fields[xsize][ysize];

I want to change it in function

    void MoveLeft(pacman *Pacman, field **fields,int **play)

But when I send it like this

     MoveLeft(&Pacman,fields,play);

I've got an error.

field - structure

     typedef struct
     {
    blossom blossoms;
    wall walls;
     }field;

where blossom & wall - another structures

4
  • 6
    You got an error... cool :) what error? Commented Nov 14, 2011 at 13:23
  • Exactly, what error? PS. I am not sure, I am not really a c programmer, but you send a Pacman reference, while it expects a pointer... Commented Nov 14, 2011 at 13:27
  • I'm not sure if you need to indicate in your function prototype that **fields is a double pointer, since it's already of type field. Commented Nov 14, 2011 at 13:28
  • @MichalB.: &Pacman is a pointer, and C does not have reference types. Commented Nov 14, 2011 at 14:08

3 Answers 3

2

Although arrays and pointers are somewhat interchangeable in C, they're not exactly the same. In particular, an array of arrays and an array of pointers are laid out differently in memory.

Here's a way to make an array of pointers which refers to the same data as your existing array of arrays:

field* field_rows[xsize];
for (unsigned int i=0; i<xsize; i++) {
    field_rows[i] = fields[i];
}

Then a pointer to that field_rows array can be passed to MoveLeft:

MoveLeft(&Pacman,field_rows,play);

Another solution might be to change the declaration of MoveLeft instead, to take a pointer to array of arrays:

void MoveLeft(pacman *Pacman, field fields[xsize][ysize], int **play);

MoveLeft(&Pacman,fields,play);
Sign up to request clarification or add additional context in comments.

3 Comments

Now I've got this error Unhandled exception at 0x001f100e in pacma.exe: 0xC0000005: Access violation reading location 0x00000044. It's error for last method
That sounds like a different problem. You'd have to debug the program.
I've solve this problem. My array play[11][31] in levelinfo.h. So I simple include levelinfo.h in moveprocs.h(where fucntion moveleft is) and it works normal now
2

I guess the error is the following: two dimension array fields[xsize][ysize] is fixed size array (xsize/ysize are defines or consts) and in memory this is not look like field**, cause it's pointer to pointer to field, while fields[xsize][ysize] internally just one dimension fixed size array, where compiler handle double indexing for you.

So what you need is just define fields as field** and allocate it dynamically.

See picture for more explanation: enter image description here

4 Comments

This is wrong. field** means pointer to pointer to field. "pointer to array of pointers to field" would be field *(*fields)[]. field** is a good type to represent the array. It's just that the type conversion has to be done explicitly as pointed out by Joachim Pileborg.
Ok, to not confuse with terminology let's say that it's pointer to pointer. Internally they all are pointers to buffer of some types...
@undur_gongor IMHO you are wrong. A field[][N] and a *(field[N]) has the same memory layout. A field** and a (*field)[] as well, but different than the first one.
@glglgl: Yes, I am totally wrong (with the last 2 sentences). Sorry. Finally, aschepler got it right.
-1

While I'm not using Windows, I'm guessing your error is something similar to this:

error: cannot convert ‘field (*)[xx]’ to ‘field**’ for argument ‘2’ to ‘void MoveLeft(pacman*, field**,int**)’

A solution to this is to simply cast the fields parameter to the type the function wants:

MoveLeft(&Pacman, (field **) fields, play);

1 Comment

Right error, but terrible answer; never just "cast away the problem" - all you are doing here is ignoring the error, and telling the compiler to proceed regardless. fields, as defined as [][], is a contiguous 2D array, whereas field** is a pointer to a (implied array) of pointers. The two have very different memory layout; you can't just treat one as the other. If you do, you'll attempt to treat the actual field in the array as through it were actually pointers - if you're lucky you'll get a crash early and realize there's something wrong.

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.