2

I have the following code of declarations:

struct coord {
int x;
int y;
}
void rotateFig(struct coord[10][10]);

I need to implement rotateFig. I tried to start with following:

void rotateFig(struct coord[10][10] c)
{
   //code
}

I can`t compile it - probaly the way I transfer c in the function definition is incorrect .How should I transfer c using given signature. Thank you

1

3 Answers 3

6

Use this definition:

void rotateFig(struct coord c[10][10])
{
   //code
}

The array is the actual parameter, so the dimensions have to come after its name, not before.

Sign up to request clarification or add additional context in comments.

Comments

3

Though other answers ARE enough, I prefer passing it as a pointer and passing the dimensions with it, this is more dynamic, and is the same for the //code part:

void rotateFig(struct coord**, int, int);

void rotateFig(struct coord** c, int d1, int d2)
{
   //code
}

1 Comment

but your prototype and definition don't match...:-/
2

struct coord is a type and c is a variable of type struct coord which can hold 10 X 10 struct coord elements.

So it should be as follows

void rotateFig(struct coord c[10][10])

One thing to note when working with multi-dimension array in C is that it cannot be return back from a function. For details, read this. So its not advised to use the above format as C by default passes arguments by value and not by address.

So as @Mr.TAMER mentioned in his answer, you should use the following

void rotateFig(struct coord** c, int d1, int d2)

OTOH, you can use the following rotate code for your reference! It rotates a 2d array to 90 degrees!

#include <stdio.h>

#define N 10 
int matrix[N][N];

void display()
{
    int i, j;

    printf("\n");
    for (i=0; i<N; i++) {
        for (j=0; j<N; j++) 
            printf("%3d", matrix[i][j]);
        printf("\n");
    }
    printf("\n");

    return;
}

int main()
{
    int i, j, val = 1;
    int layer, first, last, offset, top;

    for (i=0; i<N; i++)
        for (j=0; j<N; j++)
            matrix[i][j] = val++;

    display();

    for (layer = 0; layer < N/2 ; layer++) {
        first = layer;
        last = N - layer - 1;
        for (i=first; i< last ; i++) {
            offset = i - first;
            top = matrix[first][i];

            matrix[first][i] = matrix[last-offset][first];
            matrix[last-offset][first] = matrix[last][last-offset];
            matrix[last][last-offset] = matrix[i][last];
            matrix[i][last] = top;
        }
    }

    display();
    return 0;
}

Hope this helps!

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.