0

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!

3
  • That's the lines prodAlmLiq[countAL+2][0] = txt; and prodBazLiq[countBL+2][0] = txt; where txt is a global char[20] array and the others are global char[2000][20] arrays. The warning is about converting the type of txt (in this case char* because of automatic conversion) to the type of what is on the left side of the assignment (to char, whch is an integer type). Commented Mar 25, 2022 at 21:51
  • I don't understand how "they're both char arrays of both 20 "slots" has anything to do with it. You have to use a copy function like strcpy or memcpy. You can't just assign a whole array,. You have to copy each byte. The name of the array truncates to a pointer, hence the warning you get. Commented Mar 25, 2022 at 21:53
  • 1
    Kudos to your reaction of asking about the diagnostic. Whatever you do, do not add a cast to make the warning/error go away! Commented Mar 25, 2022 at 22:02

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.