Help with the error: "incompatible types in assignment of 'char' to 'char [1000]"'


What I want to do in this program is that the user enters a character that he wants to replace and the character with which he wants to replace it. I do not know if the function is right where I make the modification, but the issue is the error that marks me that I do not know how to solve it, I have already tried several things that have occurred to me but it has not happened to me!

The error is:

incompatible types in assignment of 'char' to 'char[1000]
struct modif
{ char textorig[1000];
char textmodif[1000];
int cantremp; };

char * carga (int  *) ; 
struct modif * modifica (char *, int, char, char);

int main()
    char *texto,caracaremp,caracderemp;
    int cantcarac,op;
    struct modif *estruc;
    texto= carga(&cantcarac);

    printf("Ingrese una de las siguientes opciones:  \n");
    printf("1-Modifica \n 2-Cuenta \n 3-Busca \n 4-Salir \n");

    if (op==1)
    { printf("Ingrese el caracter que quiere reemplazar: \n");
    scanf ("%c", &caracaremp);
    printf("Ingrese el caracter de reemplazo \n");
    scanf("%c", &caracderemp); 
    estruc= modifica(texto,cantcarac,caracaremp,caracderemp);
    printf ("El texto original es: %s", estruc->textorig);
    printf("El texto modificado es: %s", estruc->textmodif); }

    return 0; 

char * carga (int *n)
    char s[1000];
    printf("Ingrese el texto presione TAB y ENTER para finalizar:  \n");
    scanf ("%[^\t]",&s); 
    return s; }

struct modif * modifica (char *cadena, int cc, char car, char cdr)
    struct modif arr;
    int cont=0;


    for (int i=0; i<=cc; i++)
    { if (*cadena[i]=='car')
      { *cadena[i]='cdr';
        cont=cont+1;} }
      return arr; }
asked by Maca Igoillo 02.02.2017 в 14:23

1 answer


First, the errors. In the function carga( ) :

scanf( "%[^\t]", &s );

You can not pass the address of an array. The arrangement is, in itself, a pointer. It should be scanf( "%[^\t]", s ); .

In that same function, a little lower, it returns a local variable to the function. That's an indifferent behavior and it will give you problems.

Then, in modif( ) :

arr.textorig = cadena;

The chains are not copied like this. You must use any of the functions indicated for it, such as strcpy( ) , leaving strcpy( arr.textorig, cadena ); .

if( cadena[i] == 'car' ) {
  cadena[i] = 'cdr';

I do not know what you intend to do like that. Both car as cdr are characters. Both the comparisons and the assignments are direct:

if( cadena[i] == car ) {
  cadena[i] = cdr;

Then, another attempt to copy a string incorrectly:

arr.textmodif = *cadena;

The correct thing to do would be to use a function: strcpy( arr.textmodif, cadena ); .

At the end of that function modif( ) , you return an internal variable to the function again. The above, indefinite behavior.

To be able to return arrays created in a function, you are already indicated in EDIT 2 of a previous question.

How to assign an array returned by a function to an array of the main program?

To return struct or other types, it is similar:

struct modif *arr = (struct modif *)malloc( sizeof( struct modif ) );

Of course, being a pointer, the rest of the code will fail you, and you will have to adapt the operations.

answered by 02.02.2017 / 15:01