1

I was never able to fully understand what is going on in the background of passing pointers in functions and how you use them. Right now Im coding a microprocessor in c. The main goal is to create a snake type game that uses an accelormeter. Needless to say my code is filled with errors dealing with pointer implantation.

Let me try to break it down. First I have an union

    typedef union
{
    UINT8       byte;

    struct
    {
        UINT8   x  : 4;
        UINT8   y  : 4;
    };

} snake;

The union stores the x and y location of a node in the snake. I use this method so when I check if the byte exists I can do it using the byte. Anyway I initialize it as an array "snake location[64]" which is in the main function. I also have int's of the length and the head and tail of the snake. However trying to pass the union or anything else is where im having the problem. So far my prototypes for the functions are

void randGen(UINT8* x, UINT8* y);
void nextMove(snake* body, int* length, int* head, int* tail);
BOOL setLocation(snake* body,snake temp, int* length, int* head, int*tail, BOOL* newTerm);
void clearLocation(snake* body,snake temp, int* length, int* tail, BOOL* repeat);
void reset(int* length, int* index, UINT8* location);
UINT8 readDirection();
void gameOver();

Here is a snippit of the nextMove function. I pretty much just have more else if statements with more or less the same code so to keep things simple.

void nextMove(snake* body[], int* length, int* head, int* tail){
    UINT8 direction;
    int i;
    snake temp;
    temp = *body[*head];
    do {
    direction = readDirection();
    } while(direction != 4);
    if(direction == 0) { //up
        if(temp.y == 0) gameOver();
        temp.y -= 1;
        for(i = 0; i < *length; i++) {
            if (body[i]->byte == temp.byte) gameOver();
        }
        if(temp.byte == locOfApple.byte) {

        }
        else {
            clearLocation(body, *temp, length, tail, FALSE);
            setLocation(body, *temp, length,head,tail, FALSE);
            *body[*tail] = temp;
            head = tail;
            if(++tail >= length) tail = 0;

        }

    }

Mostly I just need to know how to set the prototype up, how to use the pointer as a value in the function, and what to pass the function. If passing an array (which I believe is different than passing an int) is different, if someone can explain a good way of thinking about it that would really help me grasp this concept.

Any help would be appreciated :)

Brian
  • 41
  • 5
  • possible duplicate of [Passing pointers of arrays in C](http://stackoverflow.com/questions/502059/passing-pointers-of-arrays-in-c) – Bo Persson Mar 22 '12 at 09:47
  • See also [What are the barriers to understanding pointers and what can be done to overcome them?](http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome) – Bo Persson Mar 22 '12 at 09:48
  • 1
    you declare `void nextMove(snake* body, ...);` but you define `void nextMove(snake* body[], ...)`, that's what confuses me, and probably the compiler too. – hroptatyr Mar 22 '12 at 11:00

2 Answers2

0
void nextMove(snake* body[], int* length, int* head, int* tail) 

should write like this

void nextMove(snake* body, int* length, int* head, int* tail) 

or

void nextMove(snake body[], int* length, int* head, int* tail)
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
laifjei
  • 606
  • 2
  • 7
  • 16
0

I figured it out. I needed to setup the union like

typedef union
{
    UINT8       byte[64];

    struct
    {
        UINT8   x  : 4;
        UINT8   y  : 4;
    }cord[64];

}body;

That allows me to pass it as an array and see all the values.

Brian
  • 41
  • 5