Incompatible types?

3

I'm passing 3 parameters: a vector with the names of 30 cities, one with the x coordinates of the respective cities and the other with the y coordinates.

I need to print on the screen the cities to the north, south, etc. I'm still testing. I am trying to assign the value of the city to the variable char cidade that I created (it only has 3 indexes because it will be used in the tiebreaker). However, I'm having an error:

  

"error: incompatible types when assigning to type 'char [150]' from type 'char *'" and wanted to know what the problem was.

void funcao(char cidadeXY[][150], int coordenadasX[], int coordenadasY[])
{
    int i;

    struct
    {
        int norte, sul, leste, oeste, centro;
        char cidade[150];
    }d[30];

    for(i = 0; i < 30; i++);
    {
        d[i].cidade = cidadeXY[i];
        printf("%s\n", d[i].cidade);
    }
}

As requested, declarations and the function call:

char cidadeXY[30][150];
void funcao(char cidade[][150], int coordenadasX[], int coordenadasY[])
funcao(cidadeXY, coordenadasX, coordenadasY);

The code is great and a lot of it has nothing to do with this function, so I just put the declarations and the call.

    
asked by anonymous 07.12.2014 / 02:37

3 answers

3

Only with this excerpt may it not be telling you where the problem is. The problem may be in the function call, it may be in the declaration of this array . It is not possible to know only with the information provided mainly where the error occurs.

This multidimensional array ( cidadeXY ) might not do what you expect.

The for had a ; ending it before executing any loop . It may be that the problem is just this one. After all the i used in the only interaction that would be performed would be worth an undefined value.

I do not know if it will do anything else but this whole code does not make much sense. This struct and the d variable itself is useless. Divide and conquer. Solve one problem at a time. For this I removed the part that does nothing useful as well. Then if everything is right and this is necessary for something you add.

void funcao(char cidadeXY[][150], int coordenadasX[], int coordenadasY[]) {
    for (int i = 0; i < 30; i++) printf("%s\n", cidadeXY[i]);
}

I placed GitHub for future reference .

    
07.12.2014 / 02:53
1

Really as said the Maniero there is a ";" in that it kills its for. All code runs once, regardless of for validation.

for(i = 0; i < 30; i++); <---- Esse ponto e virgula aqui!
{
    d[i].cidade = cidadeXY[i];
    printf("%s\n", d[i].cidade);
}

With this ; for does not execute anything, and the snippet that should be inside the for is executed independent of his once. As if your code looks like this:

for(i = 0; i < 30; i++)
{
    ;
}

// Isso aqui é executado independente do for
{
    d[i].cidade = cidadeXY[i];
    printf("%s\n", d[i].cidade);
}

To learn more about this see this link Look for "Nested Block Scope"

In addition to manipulating strings , in the excerpt:

d[i].cidade = cidadeXY[i];

You only point d[i].cidade to the string you want. This can bring unexpected behavior to your program. If you really want to copy the content you should do what pmg recommended:

strcpy(d[i].cidade,cidadeXY[i]);

I think it's the most recommended.

That way your code would look like this:

void funcao(char cidadeXY[][150], int coordenadasX[], int coordenadasY[]) {
    int i;
    struct {
        int norte, sul, leste, oeste, centro;
        char cidade[150];
    } d[30];

    for(i = 0; i < 30; i++) {
        strcpy(d[i].cidade,cidadeXY[i]);
        printf("%s\n", d[i].cidade);
    }
}
    
07.12.2014 / 13:24
0

To assign values to character arrays (a strings) you must use strcpy() .

#include <string.h>

...

        // d[i].cidade = cidadeXY[i];
        strcpy(d[i].cidade, cidadeXY[i]);
    
07.12.2014 / 10:06