Error compiling in GCC linux

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

 int main(int argc, char const *argv[])
 {
    int a;
     a=50;

   //imprimo a
   printf("El valor de a: %i \n",a);
   printf("La dire de a: %i \n",&a);

   //creo 2 punteros
   int *p1, *p2;

  //p1 tiene de dato la direc de a (&a)
   p1=&a;

   //copio el dato de p1 en p2
   p2=p1;

   //imprimo p1
   printf("valor de p1: %i \n",p1);
   printf("Direccion de p1: %i \n",&p1);
   printf("Apunta p1: %i \n",*p1);

  //imprimo p2
  printf("valor de p2: %i \n",p2);
  printf("Direccion de p2: %i \n",&p2);
  printf("Apunta p2: %i \n",*p2);

  return 0; 
}

When compiling from the ubuntu terminal with gcc, it gives me an error:

        punteros07.c: In function ‘main’:
      punteros07.c:12:27: warning: format ‘%i’ expects argument of 
      type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
     printf("Direccion de a: %i \n",&a);
                             ^
      punteros07.c:24:26: warning: format ‘%i’ expects argument of 
      type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
      printf("valor de p1: %i \n",p1);
                           ^
      punteros07.c:25:30: warning: format ‘%i’ expects argument of 
      type ‘int’,  
      but argument 2 has type ‘int **’ [-Wformat=]
      printf("Direccion de p1: %i \n",&p1);
                               ^
    punteros07.c:29:25: warning: format ‘%i’ expects argument of type 
    ‘int’, 
    but argument 2 has type ‘int *’ [-Wformat=]
    printf("valor de p2: %i \n",p2);
                         ^
    punteros07.c:30:29: warning: format ‘%i’ expects argument of type 
    ‘int’, 
    but argument 2 has type ‘int **’ [-Wformat=]
    printf("Direccion de p2: %i \n",&p2);
                             ^

If I compile it with codeblocks it works perfectly, what would be the error for which I can not compile it in gcc?

    
asked by Gabriel Forzza 29.03.2017 в 18:54
source

1 answer

1

What you get are not mistakes, they are warnings, your program will work anyway, although it may not produce the correct result, but keep in mind that a int is not the same as a int * .

You are currently passing through a parameter to printf a pointer, its address and the value it points to.

To display pointers correctly, use the format specifier %p in the following way:

printf("El puntero: %p\n", p1); // Muestra la dirección de p1.

This happens because the memory addresses are of different sizes depending on the platform and %i or %d expect a signed integer per parameter and not an unsigned number.

The same thing happens with the following instruction:

printf("Direccion de p2: %i \n",&p2);

When using the & operator in a pointer, you return its type in a pointer to a pointer , that is, int * is not the same as int ** , the latter is used to emulate what is "Pass by reference" in C.

If you do:

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

void asignar_NULL(int **ref) {
    if (ref && *ref)  // Aseguremonos de que el puntero existe.
        free(*ref)
    *ref = NULL;
}

int main(void) {
    printf("Colocando a INT\n");
    int *INT = malloc(sizeof(int));
    asignar_NULL(&INT); // pasamos a INT por "referencia".
    *INT = 4; // Error! INT ahora es NULL, segmentation fault aquí.
    return 0;
}

Notice that I have declared a pointer to int and I have converted it into a _ pointer to int and thus be able to use that variable.

See:

answered by 29.03.2017 в 19:00