Im trying to copy a char array to a bidimensional char array, but i keep getting this error message: assignment makes integer from pointer without a cast [-Werror=int-conversion]
.
It's weird because they're both char arrays of both 20 "slots"
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int entrada(char arg);
void procesado(int input);
char txt[20];
int prodAlm, prodPan, prodFre, prodBaz, prodDes;
char prodAlmLiq[2000][20];
char prodBazLiq[2000][20];
int main(){
while(1){
procesado(entrada(0));
}
return 0;
}
int entrada(char arg){
int res;
switch(arg){
case 0:
printf("Ingrese el codigo del producto: ");
scanf("%d", &res);
return res;
break;
case 1:
printf("Ingrese el codidigo del producto: (max. 12 caract.)");
scanf("%s", txt);
return 0;
break;
case 2:
printf("Ingrese el valor del producto: $");
scanf("%d", &res);
return res;
break;
case 3:
printf("!!Ingrese una descripcion breve (max. 12 caract.): !! [NOT WORKING, INPUT ANY NUMBER]");
scanf("%s", txt);
return 0;
break;
case 4:
printf("Ingrese el stock disponible: ");
scanf("%d", &res);
return res;
break;
case 5:
printf("Ingrese la cantidad en gondolas disponible: ");
scanf("%d", &res);
return res;
break;
default:
if (arg == 'u'){
printf("Saliendo...");
return res;
} else {
printf("Ingrese una opcion correcta!\n\r");
}
break;
}
return 0;
}
void procesado(int input){
static int countAL, countBL;
static int returning = 0;
float tmp;
if (input == 'u'){
//returning = 1;;
}
if (!returning){
switch(input){
case 10:
prodPan ;
break;
case 20:
prodAlm ;
prodAlmLiq[countAL][0] = countAL 1;
tmp = entrada(2);
prodAlmLiq[countAL 2][0] = txt; //Pregunta por el precio del prod
tmp = (float) prodAlmLiq[countAL 2][0];
if(tmp >= 4.5 && tmp <= 5.5){
prodAlmLiq[countAL 1][0] = entrada(1); //Pregunta codigo del prod
prodAlmLiq[countAL 3][0] = entrada(3); //Pide descripcion del prod
prodAlmLiq[countAL 4][0] = entrada(4); //Pregunta el stock disponible del prod
prodAlmLiq[countAL 5][0] = entrada(5); //Pregunta cantidad en gondola del prod
countAL = countAL 6; //Da la vuelta a la estructura del arreglo para empezar una nueva linea
}
break;
case 30:
prodFre ;
break;
case 40:
prodAlm ;
prodBazLiq[countBL][0] = countBL 1;
tmp = entrada(2);
prodBazLiq[countBL 2][0] = txt; //Pregunta por el precio del prod
tmp = (float) prodBazLiq[countBL 2][0];
if(tmp >= 9 && tmp <= 11){
prodBazLiq[countBL 1][0] = entrada(1); //Pregunta codigo del prod
prodBazLiq[countBL 3][0] = entrada(3); //Pide descripcion del prod
prodBazLiq[countBL 4][0] = entrada(4); //Pregunta el stock disponible del prod
prodBazLiq[countBL 5][0] = entrada(5); //Pregunta cantidad en gondola del prod
countBL = countBL 6; //Da la vuelta a la estructura del arreglo para empezar una nueva linea
}
break;
default:
prodDes ;
break;
}
printf("----------------------------------------\n\r Producto Agregado!\n\r----------------------------------------");
}
}
errors ocurr at 98,35 and 115,35.
code is not finished yet, but i wont keep going until i can solve that error.
Thanks for helping!
CodePudding user response:
right here
prodAlmLiq[countAL 2][0] = txt;
the left side is a char
, the right side is a char*
its not clear what you are actually trying to do though. Maybe you want to copy the string in txt to prodAlmliq[countAL ]
if so
strcpy( prodAlmLiq[countAL 2] , txt);
is what you need
CodePudding user response:
The implicated lines are
prodAlmLiq[countAL 2][0] = txt;
and the similar
prodBazLiq[countBL 2][0] = txt;
prodAlmLiq
and prodBazLiq
are each defined as a char[2000][20]
, so in each case, the item being assigned is a single char
. For its part, txt
is defined as a char[20]
, so it does not make sense to try to assign it to a single char. The error message reflects the fact that the txt
array is automatically converted to a pointer in this context (as in most others), and although C permits that to be converted onward to a char
, that's very unlikely to be what you really wanted, and standard C in any case requires a cast to perform that conversion.
My best guess is that you want to copy the contents of txt
into corresponding elements of the big arrays. supposing that txt
contains a null-terminated string, you would use strcpy
for that. Example:
strcpy(prodAlmLiq[countAL 2], txt);