Created
January 31, 2013 14:19
-
-
Save mapio/4683150 to your computer and use it in GitHub Desktop.
Non si può insegnare C al primo anno!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main( int argc, char *argv[] ) | |
{ | |
printf( "Salve, %s!\n" ); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int main( int argc, char *argv[] ) | |
{ | |
printf( "Salve, %s!\n", argv[ 1 ] ); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <stdio.h> | |
int main( int argc, char *argv[] ) | |
{ | |
char stringa[ 100 ]; | |
strcpy( stringa, argv[ 1 ] ); | |
printf( "Salve, %s!\n" ); | |
return 0; | |
} |
gcc -Wall strana.c -o strana
segnalerebbe un warning per la printf usata male
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uno studente, cui era assegnato il compito di scrivere una versione di "Salve, mondo!" che salutasse invece di "mondo" quanto passato come primo argomento sulla linea di comando (vedi la soluzione intesa
giusta.c
) ha consegnato la soluzionestrana.c
.Evidentemente errata (notate che alla riga 8 la stringa di formato include un
%s
, ma manca il secondo argomento allaprintf
)!Ebbene, grazie alla precedente chiamata a
strcpy
che sporca lo stack, funziona (su GNU/Linux, ma non su OSX), ossia se invocata comestampa correttamente "Salve, ARG!"…
Rimuovendo la copia inutile, si ottiene la versione
errata.c
che non solo è errata, ma esibisce anche un comportamento errato.