I am trying to compile my code but I am not able to compile it. I am using VS 2010 and I get this message:
‘Tel_zoznam.exe’: Loaded ‘C:\Windows\SysWOW64\msvcr100d.dll’, Symbols loaded.
Run-Time Check Failure #3 – The variable ‘p_prvy’ is being used without being initialized.
It stops at p_prvy->next = NULL;
Here is my code:
#include "stdafx.h"
#define MAX 31
typedef struct ZOZNAM{
char meno[MAX];
char priezvisko[MAX];
char cislo1[MAX];
char cislo2[MAX];
char cislo3[MAX];
struct ZOZNAM *next;
} ZOZNAM;
int main(void){
char c;
ZOZNAM * p_prvy;
ZOZNAM * p_akt;
p_prvy->next = NULL;
int z;
p_akt=p_prvy;
printf(" Pre pridanie kontaktu do zoznamu stlacte 'p'\n Pre vypis zoznamu zadajte 'v'\n Pre ukoncenie programu zadajte 'k'\n");
z=pocet_zaznamov();
printf("%d",z);
while(1==1){
scanf("%c",&c);
switch(c){
case 'p': vlozit(p_akt); break;
case 'v': vypis(p_prvy); break;
case 'n': nacitanie(p_akt); break;
}
}
return 0;
}
In your
main()function, you have the following declaration of thep_prvyvariable:Almost immediately after that, you say:
Which means you are using
p_prvyvariable. However, it is not being initialized and so has unspecified value. This is called Undefined Behavior, or simply UB.What you need to do is initialize that pointer. For example, by allocating some memory:
You may also have it initialized to NULL, but then dereferencing NULL will take your process down.