error: ISO C ++ forbids comparison between pointer and integer [-fpermissive]

// Liberías

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

//Tengo el siguiente struct para nuevo1

struct proveedores { 
    int Codigo; 
    char Nombre [20]; 
    char Razon_Social [30];
    int Numero_Rut;
    int Direccion_Postal;
    int Telefono;

The High Providers feature

void ALTAS_proveedores()
    printf("\t\t\t*ALTAS DE PROVEEDORES*\n");
        printf("Ingrese el Nombre del Proveedor (Para terminar ingrese 0): ");
        scanf("%s", nuevo1.Nombre);
        if(nuevo1.Nombre!=NULL || nuevo1.Nombre != 13) //<- error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
            printf("Ingrese el Codigo del Proveedor: ");
            printf("Ingrese el nombre del proveedor");
            scanf("%s", nuevo1.Nombre);
            printf("Ingrese la Razon Social: ");
            scanf("%s", nuevo1.Razon_Social);
            printf("Ingrese Nº RUT: ");
            printf("Ingrese la Direccion Postal: ");
            printf("Ingrese el Telefono: ");
        }//cierro if
    }//cierro while
    printf("Alta/s realizada, pulse una tecla para regresar al menu principal");
}//cierro función ALTA_Proveedores

Throw error saying


error: ISO C ++ forbids comparison between pointer and integer [-fpermissive]

What could be happening?

asked by Alejandro Caro 02.10.2017 в 00:42

1 answer

if( nuevo1.Nombre != NULL || nuevo1.Nombre != 13 )

You can not compare a pointer with an integer. The error is very explicit.

In C, the array does not exist at the same level as in other languages. When you do

struct proveedores { 
  int Codigo; 
  char Nombre [20]; 
  char Razon_Social [30];

char Nombre is not an fix ; it is a pointer to the first hollow or space of the 20 that the compiler has generated.

And the pointers can only be directly compared to each other, or with a special numeric value : the 0 (which is what% NULL is worth) ).

Therefore, nuevo1.Nombre != NULL is always true . For the simple fact that nuevo1 already exists.

The error tells you why NULL is 0 , and the comparison if it is valid. But 13 is not 0 . It's like comparing apples and pears.

I guess what you're trying to do is check if the string has been read correctly. To do this, you have to use strlen( ) .

if( strlen( nuevo1.Nombre ) ) {

That function returns the length in bytes of the string, and is declared in the <string.h> header.

Note : What you do in the end, call back main( ) , is curious . When the function ends, returns it only to the point from which it was called; which indicates that the logic of the rest of your application does not end well.

answered by 02.10.2017 в 05:22