0

I'm having a problem dealing with an array in C. You see, this is a portion of my code which basically reads a file and organizes coordinates of the vertices of parcels of the 3rd-levels administrive divisions in Portugal - which we call Freguesias. In this part of the exercise, I need to write the name of all 2rd-levels administrive divisions - Concelhos (which is already well defined in my code in the array Cartography cartography, that isn't the problem) that appear in the file.

I want to do a function that shows what Concelhos appear in the file and I want to write with this exact subfunctions and functions so I can change some things later, but for some reason it doesn't printf the strings in "command_list_concelhos", it just prints NULL strings. I don't know why this happens, specially since it does rightly so if I do a printf inside and outside the for in "read_string_concelhos".

Sorry if this question is wrongly explained, too big or just a small detail that I am missing, but I don't have a better way to explain it...

#define MAX_STRING 256
#define MAX_NAMES  50

typedef char String[MAX_STRING];

typedef struct {
    String list[MAX_NAMES];
    int n_strings;
}   StringList;

int read_string_concelhos(StringList s ,Cartography cartography, int n)
{
    int i, j=1;
    strcpy (s.list[j-1], cartography[0].identification.concelho);
    for ( i = 0 ; i < n ; i++){
        if ( strcmp(cartography[i].identification.concelho, s.list[j-1]) != 0){
            strcpy(s.list[j] , cartography[i].identification.concelho);
            j++;
        }
    }
    return j; // n_strings
}

void command_list_concelhos(Cartography cartography, int n)
{
    StringList s;
    s.n_strings = read_string_concelhos(s, cartography, n);
    int i;
    for(i = 0; i < s.n_strings; i++ )
    {
        printf("\n", s.list[i]);
    }
}

Fail

How it should look like

3
  • 2
    When the code passes s to read_string_concelhos, s is a copy of the structure. So any changes made to s only affect the copy. The s in command_list_concelhos is not changed when read_string_concelhos changes its copy of s. Commented Dec 9, 2018 at 2:17
  • @user3386109 Would you like to turn that into an answer? Commented Dec 9, 2018 at 8:32
  • @user3386109 I do understand what's the problem, but can you tell me how can I fix this please? I suppose I'm missing some detail. Commented Dec 9, 2018 at 22:10

1 Answer 1

1

int read_string_concelhos(StringList s ,Cartography cartography, int n)
should be changed to
int read_string_concelhos(StringList* s ,Cartography cartography, int n)

And inside the function int read_string_concelhos(StringList* s ,Cartography cartography, int n) { ... }, all s.list[...] should be changed to s->list[...]. In this way, parameter s is a pointer therefore strcmp would paste to s declared in command_list_concelhos which is the desired behaviour.

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

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.