Zeiger Probleme

norline

Mitglied
Hallo,
wieso funktioniert das nicht?
#include <string>
using namespace std;
struct
{
int x;
}*Erg;
main()
{


Erg->x=4;
FILE* Result=NULL;
Result =fopen("d:/temp/PointerTest.txt", "w+");

fprintf(Result,"Erg: =%d \n",Erg->x);
return 0;
}

bekomme immer logische fehler?
 
So herum funktionierts:

Code:
typedef struct _Erg 
{
int x;
}Erg;

main()
{

// create new Pointer to Erg
Erg *e = new Erg;
e->x=4;
FILE* Result=NULL;

// No Slashes here but BACKSlashes - you are in Win32 
Result =fopen("c:\\PointerTest.txt", "w+");

// Test, if File is open
if( Result != 0 ) {
fprintf(Result,"Erg: =%d \n",e->x);

// Dont forget to close the File 
fclose(Result);

} // End if

delete e;
return 0;
}
 
Zurück