Incompatible types?

Asked

Viewed 272 times

3

I am passing 3 parameters: a vector with the name 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 further north, south, etc. I’m still doing tests. I’m trying to assign the city value 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 a mistake:

"error: incompatible types when assigning to type ľchar[150]' from type ¡char *'" and wondered what the problem is.

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, the statements and the function call:

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

The code is big and a lot of it has nothing to do with that function, so I just put the statements and the call.

  • The signature of the function should be: void funcao(char** cidadeXY, int coordenadasX[], int coordenadasY[]). If this settles let me know that I add an official response.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

3

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

That one array multidimensional (cidadeXY) may not do what you expect.

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

I don’t know if it’s going to do anything else but this whole code doesn’t make much sense. This struct and the variable itself d It’s no use. Divide and conquer. Solve one problem at a time. That’s why I took out the part that doesn’t do anything useful either. Then if all 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 put in the Github for future reference.

  • The function call is: function(cityXY, coordinate sX, coordinate sY); and the statement: void function(char cityXY[][150], int coordinate sX[], int coordinate sY[]); Thanks for pointing ; after for the for. I didn’t realize.

  • ah and this code is not working either

  • But how is the variable declaration cidadeXY? It’s certainly not working, there are other problems. Edit your question and put other parts of the code.

1

Really like Maniero said there is a ";" in what kills your for. All code is executed only once regardless of the 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 that ; the for does not execute anything, and the chunk that should be inside the is executed independent of it once. As if your code were 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 Search for "Nested Block Scope"

In addition to handle strings, in the stretch:

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

You just point out d[i].cidade to the string that 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 believe I’m 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);
    }
}
  • I had already fixed the loop for. Thank you very much! I totally forgot about this strcpy function!

0

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

#include <string.h>

...

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.