In case 3, I enter the value "1" in xedit and at the output I get 1244956. Why?

//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop //--------------------------------------------------------------------------- #pragma argsused //--------------------------------------------------------------------------- #include <stdio.h> #include <io.h> #include <conio.h> //--------------------------------------------------------------------------- struct TZap { char FIO[30]; int god_r; int n_gr; int ocen_fiz, ocen_mat, ocen_inf, ocen_him; double s_b; } Zap; int size = sizeof( TZap ); FILE *Fz, *Ft; char File_Zap[] = "zapisi.dat"; char File_Rez[] = "rezult.txt"; void Out( TZap ); int main( int argc, char *argv[] ) { int kod, D_f, i = 0, j, kol, x = -1; char afio[30]; int agod_r, an_gr, aocen_fiz, aocen_mat, aocen_inf, aocen_him, as_b; int xedit = 0; long len; TZap st, *mas_Z; Ft = fopen( File_Rez, "w" ); char fio_edit[30]; while( true ) { puts( "\n\n 1 - Create\n 2 - Add\n 3 - Edit\n 4 - View\n\n 0 - Exit" ); printf( "\n\t" ); scanf( "%d", &kod ); switch( kod ) { case 1: if( ( Fz = fopen( File_Zap, "wb" ) ) == NULL ) { puts( "\n Create ERROR! Press any key!" ); getch(); continue; } fclose( Fz ); printf( "\n Create New File %s !\n", File_Zap ); break; case 2: Fz = fopen( File_Zap, "ab" ); printf( "\n FIO - " ); fflush( stdin ); printf( "\n\t" ); gets( Zap.FIO ); printf( " Year of birth - " ); printf( "\n\t" ); scanf( "%d", &Zap.god_r ); printf( " Group number - " ); printf( "\n\t" ); scanf( "%d", &Zap.n_gr ); printf( " Physics - " ); printf( "\n\t" ); scanf( "%d", &Zap.ocen_fiz ); printf( " Mathematics - " ); printf( "\n\t" ); scanf( "%d", &Zap.ocen_mat ); printf( " Computer science - " ); printf( "\n\t" ); scanf( "%d", &Zap.ocen_inf ); printf( " Chemistry - " ); printf( "\n\t" ); scanf( "%d", &Zap.ocen_him ); printf( " Mark - " ); printf( "\n\t" ); scanf( "%lf", &Zap.s_b ); fwrite( &Zap, size, 1, Fz ); fclose( Fz ); break; case 3: if( ( Fz = fopen( File_Zap, "rb" ) ) == NULL ) { puts( "\n Open ERROR! Press any key!" ); getch(); continue; } printf( "\n Vvedite FIO" ); fflush( stdin ); printf( "\n\t" ); gets( fio_edit ); Fz = fopen( File_Zap, "rb" ); D_f = fileno( Fz ); len = filelength( D_f ); kol = len / size; mas_Z = new TZap[kol]; for( i = 0; i < kol; i++ ) { fread( ( mas_Z + i ), size, 1, Fz ); } fclose( Fz ); for( int i = 0; i < kol; ++i ) if( strcmp( mas_Z[i].FIO, fio_edit ) == 0 ) { x = i; break; } else { x = -1; } if( x == -1 ) { puts( "\tSovpadenii net" ); } else { printf( " Edit:\n 1 - FIO\n 2 - Year of birth\n 3 - Group number\n 4 - Physics\n 5 - Mathematics\n 6 - Computer science\n 7 - Chemistry\n 8 - Mark\n" ); printf( "\n\t" ); scanf( "%d", &xedit ); printf( "%d", &xedit ); /* switch() { case 1: printf("Vvedite FIO"); //printf("\n\t");gets(afio); break; case 0: return(0); }*/ } delete []mas_Z; break; case 4: if( ( Fz = fopen( File_Zap, "rb" ) ) == NULL ) { puts( "\n Open ERROR! Press any key!" ); getch(); continue; } printf( "\n\t--------- Informations ---------\n" ); fprintf( Ft, "\n\t--------- Informations ---------" ); while( 1 ) { if( !fread( &Zap, size, 1, Fz ) ) { break; } Out( Zap ); } fclose( Fz ); break; case 0: fclose( Ft ); return( 0 ); } } return( 0 ); } void Out( TZap z ) { printf( "\n %20s, %d, %d, %d, %d, %d, %d, %1f\n\n\t", z.FIO, z.god_r, z.n_gr, z.ocen_fiz, z.ocen_mat, z.ocen_inf, z.god_r, z.s_b ); fprintf( Ft, "\n %20s, %d, %d, %d, %d, %d, %d, %1f\n\n\t", z.FIO, z.god_r, z.n_gr, z.ocen_fiz, z.ocen_mat, z.ocen_inf, z.god_r, z.s_b ); } //--------------------------------------------------------------------------- 

    1 answer 1

    Because you need to output not the address of the variable, but its value:

     printf( "%d", &xedit ); // <<< неправильно printf( "%d", xedit ); // <<< правильно 

    And any normal compiler should obscure the wrong construction. For example:

     warning: format '%d' expects argument of type 'int', but argument 2 has type 'int*' [-Wformat=] printf( "%d", &xedit ); ^ 
    • So it gives an error at the place where the number is entered when the program is running. Access violation - aaa
    • @ Eugene, what does this have to do with your question? I answer the question that was asked, and not some other one :) - PinkTux
    • So I create a new question? Why an error at the place of entry? Are you kidding me? - aaa
    • The compiler is not obliged to curse, because The format string may not be known at all at the time of compilation. But if it is fixed, good implementations check the type of the arguments for matching the format string. - αλεχολυτ
    • At the place of input error, help - aaa