Emmaaaaa
Emmaaaaa

Reputation: 3

assignment incompatible pointer type (WARNING)

The problem is every time I try to submit this code this message shows up:

assignment from incompatible pointer type [enabled by default]

The warning is caused by aux=q; How should I make them compatible?

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    int a, b, c, d;
}
t_cuatro;

void order(t_cuatro * q);

int main()
{
    t_cuatro x = {5,6,2,8};
    printf("%d, %d, %d, %d\n", x.a, x.b, x.c, x.d);
    ordenar(&x);
    printf("%d, %d, %d, %d\n", x.a, x.b, x.c, x.d);
    return 0;
}

void order(t_cuatro  *q)
{
    int * aux;

    int aux2;

    int i,j;

    aux=q;

    for(i=0;i<4;i++)
    {
        for(j=i+1;j<4;j++)
        {
            if(*(aux + i)>*(aux + j))
               {
                   aux2 = *(aux+i);
                   *(aux+i) = *(aux+j);
                   *(aux+j) = aux2;

               }
        }
    }


}

Upvotes: 0

Views: 412

Answers (1)

P.P
P.P

Reputation: 121397

int * and struct t_cuatro * are not compatible. You can't "make" them compatible.

It looks like you are attempting to sort the contents in a struct...but you haven't actually used the members in order(). I suggest you use an array (instead of four variables: a, b, c, and d):

typedef struct
{
    int arr[4];
}
t_cuatro;

Then in order() you can assign it to an int *:

void order(t_cuatro  *q)
{
    int *aux = q->arr;
    /* remove the assignment aux = q; */
 ....

Print the array as:

for (size_t i = 0; i < sizeof x.arr/sizeof x.arr[0]; ++i)
printf("%d ", x.arr[i]); 

Upvotes: 4

Related Questions