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]);
}
}
storead_string_concelhos,sis a copy of the structure. So any changes made tosonly affect the copy. Thesincommand_list_concelhosis not changed whenread_string_concelhoschanges its copy ofs.