+ 1

C compiler dependent error

https://code.sololearn.com/cKTRTpkYeVYY/#c this code runs fine on online compilers like gdb, but crashes on VS or Tcc compiler for VS Code. The error seems to be line 59. But its just a simple printf. So maybe its the function add_planet or add_mond. Can' t find it...

20th Jun 2019, 8:55 AM
Maxim Schiffmann
Maxim Schiffmann - avatar
8 Réponses
+ 2
What is line 58 supposed to do? printf("%*c", strlen(anchor[i]->name), ' '); strlen is of type size_t, the format specifier is %zu (I think), but you're using one format specifier %*c with two arguments, suppressing the first char of what is a size_t and adding a space? Not saying that what you're doing is wrong, I just don't get it.
20th Jun 2019, 9:30 AM
Anna
Anna - avatar
+ 2
@Anna try this out: //////////////////////////////// int i = 5; printf("%*s", i, " "); // same as ("%*c", i, ' '); printf("Hi!\n"); //////////////////////////////// In my code I want to make sure that the moons are listed under the planet km. so i use strlen(anchor[i]->name) to get the lenght of the planet name. Go to https://www.onlinegdb.com/ compiler and try my code out. this is also a good method for loops, try this: #include <stdio.h> int main() { int i = 0; int lenght = 5; for ( i = 1; i <= lenght; i++) { printf("%*s\n", i, "*"); } return 0; }
20th Jun 2019, 10:40 AM
Maxim Schiffmann
Maxim Schiffmann - avatar
+ 1
@Gordie Thank you, instant fix it with this line: for(int j = 0; j < MAX_MONDE; j++) { anchor[i]->monde[j] = NULL; } I have to set them to zero, because the structs are predetermined in the exercise.
20th Jun 2019, 11:05 AM
Maxim Schiffmann
Maxim Schiffmann - avatar
0
@Gordie How can i fix this?
20th Jun 2019, 10:51 AM
Maxim Schiffmann
Maxim Schiffmann - avatar
0
Can you show me, how the direct way?
20th Jun 2019, 7:59 PM
Maxim Schiffmann
Maxim Schiffmann - avatar