Error: assignment makes integer from pointer without a cast,

1

I need your help with this small part of my program. I have to create a database of products in C language and one of the functions that we have to use is called "initializar.c", its purpose is to initialize the field "product" with '\ 0' (the null character) for all the info info array entries (arrays of type struct). At first I did it this way:

/*Archivo del tipo .h (inv.h); contiene las librerías a utilizar en el 
programa y la estructura con sus campos a usar.*/
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

//Se crea la esctructura con los siguientes campos.
typedef struct{
       char producto[30];
       float coste;
       unsigned existencia;
}inv; 

 inv info_inv[100];
-------------------------------------------
 #include "inv.h"

 //Prototipos:
 FILE* mifopen(const char *, const char *);
 void* mimalloc(unsigned);
 unsigned menu1(void);
 unsigned menu2(void);
 void inicializar_lista(void);  

int main(){
    unsigned opc;
    //inv info_inv[100], *t_inv; 

   /*Realizamos un bucle indefinido para que el programa continue cuantas 
   veces el usuario desee. Luego, llamamos el menu principal (menu1.c).*/
   while(1){
        opc = menu1();

       if(opc == 1){
         //t_inv = &info_inv[100]; inicializar_lista(t_inv);     
         incializar_lista(void); 
      }
      else if(opc == 2){
         printf("\nVerificar: %u.\n",opc);
      }
      else {
         printf("\nERROR: Opcion NO mostrada en el Menu.\n");
         printf("\nSalida del programa.\nBye!\n"); break;  
      }


}
 system("PAUSE");   
 return 0;
}
--------------------------------------------------------
#include "inv.h"

//void inicializar_lista(inv *p){
void inicializar_lista(void){
     unsigned i;

     for(i = 0; i < 100; i++){
         //p = &info_inv[i];
         //p->producto[0]="
/*Archivo del tipo .h (inv.h); contiene las librerías a utilizar en el 
programa y la estructura con sus campos a usar.*/
#include <stdio.h> 
#include <stdlib.h>
#include <string.h>

//Se crea la esctructura con los siguientes campos.
typedef struct{
       char producto[30];
       float coste;
       unsigned existencia;
}inv; 

 inv info_inv[100];
-------------------------------------------
 #include "inv.h"

 //Prototipos:
 FILE* mifopen(const char *, const char *);
 void* mimalloc(unsigned);
 unsigned menu1(void);
 unsigned menu2(void);
 void inicializar_lista(void);  

int main(){
    unsigned opc;
    //inv info_inv[100], *t_inv; 

   /*Realizamos un bucle indefinido para que el programa continue cuantas 
   veces el usuario desee. Luego, llamamos el menu principal (menu1.c).*/
   while(1){
        opc = menu1();

       if(opc == 1){
         //t_inv = &info_inv[100]; inicializar_lista(t_inv);     
         incializar_lista(void); 
      }
      else if(opc == 2){
         printf("\nVerificar: %u.\n",opc);
      }
      else {
         printf("\nERROR: Opcion NO mostrada en el Menu.\n");
         printf("\nSalida del programa.\nBye!\n"); break;  
      }


}
 system("PAUSE");   
 return 0;
}
--------------------------------------------------------
#include "inv.h"

//void inicializar_lista(inv *p){
void inicializar_lista(void){
     unsigned i;

     for(i = 0; i < 100; i++){
         //p = &info_inv[i];
         //p->producto[0]="%pre%";
         info_inv[i].producto[0] = '%pre%';
     }    
 }
"; info_inv[i].producto[0] = '%pre%'; } }

When I compiled the program I got this error: [Warning] assignment makes integer from pointer without a cast. And I still do not see why. The parts that appear commented were my attempts to fix the problem, however, they did not work. If you can help me with that, I would really appreciate it.

    
asked by Gigi_R 24.03.2018 в 21:09
source

1 answer

1

As it is the code that you have attached, contains errors, but they are not the one that you pose in the question. The errors are:

  • In the main program, you call incializar_lista() , instead of inicializar_lista()
  • You pass void as a parameter, which is incorrect. Just do not pass anything.
  • The variable info_inv declares it globally, but when doing it in the file .h , that declaration would be repeated in each file .c that contains a #include "inv.h" , which is also not correct. The correct thing is for the variable to be declared globally only in one place (for example in the file where main() is), and in the others its declaration appears as external ( extern inv info_inv[100] ). This external declaration could go to inv.h so that all the parts of the program know about the existence of that global variable, although really only reserves a place for it in the main module.

    Better still, it would be that the variable was not global, but local of main() and that it would be passed as a parameter to the functions that need it. It seems that some of that you tried in the code commented, but it is not clear what concrete combination of code commented was the one that you tried.

  • In inv.h the prototype of the function inicializar_lista() should also be declared, otherwise when you call it from main() the compiler does not know if you are calling it correctly, with the correct parameters.
  • As you see, none of this is related to the error you mention. In fact, after solving these errors, I compile without errors or Warnings (although I can not build the executable because the function menu1() is missing)

    The error you mention perfectly could be caused by this line (which, however, is not in your code, although in the comments I see similar things):

    info_inv[i].producto[0] = "
    info_inv[i].producto[0] = "%pre%";
    
    ";

    As you can see, I have put double quotes around the "char" . This would be wrong, because in that case 'char' would be a literal string , which would be converted by the compiler into a constant of the pointer type (see my answer to another question for more details on this topic).

    In that case you would be trying to put a pointer in a %code% and could give you an error similar to the one you mention. But it is not what you have in your code, in which you have used %code% , that is, single quotes instead of double, which is the correct thing because in that case we have a constant of type %code% .

    I do not know if the code you put in is really the one you're testing. In any case, it would be important to point out which line in particular gives you the error to be able to find out what is due, since in my experiments, as I said, I could not replicate that error.

        
    answered by 24.03.2018 в 21:43