realloc的奇怪的錯誤我有一個代碼:在VS 2010
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
typedef struct NOTE
{
char NAME[50],
TELE[30];
int BDAY[3];
} NOTE;
void AddNote(NOTE * Notes, int NotesCount, NOTE * Temp)
{
Notes = (struct NOTE *) realloc(Notes, (NotesCount + 1) * sizeof(struct NOTE));
memcpy(Notes + NotesCount, Temp, sizeof(struct NOTE));
}
void main()
{
int NotesCount = 0, i = 0, f = 0;
int a;
NOTE * BLOC_NOTE, * Temp;
Temp = (struct NOTE *) malloc(sizeof(struct NOTE));
BLOC_NOTE = (struct NOTE *) calloc(0, sizeof(struct NOTE));
for(i = 0; i < 4; i++)
{
ShowInputDialog(Temp);
AddNote(BLOC_NOTE, NotesCount++, Temp);
}
}
在BLOC_NOTE的第三個要素,程序崩潰的
Notes = (struct NOTE *) realloc(Notes, (NotesCount + 1) * sizeof(struct NOTE));
VS說,我說,Windows操作系統的啓動斷點...
怎麼了?
編輯
從註釋
void ShowInputDialog(NOTE * Temp)
{
printf("Name: ");
scanf("%s", (*Temp).NAME);
printf("Telephone: ");
scanf("%s", (*Temp).TELE);
printf("Birthday: ");
scanf("%d\.%d\.\%d", (*Temp).BDAY, ((*Temp).BDAY + 1), ((*Temp).BDAY + 2));
}
看起來沒問題,所以錯誤必須在其他地方,比如'ShowInputDialog'。這是做什麼的? – 2011-05-14 17:15:38
'void ShowInputDialog(NOTE * Temp) { \t printf(「Name:」); \t scanf(「%s」,(* Temp).NAME); \t printf(「Telephone:」); \t scanf(「%s」,(* Temp).TELE); \t printf(「Birthday:」); (* * Temp).BDAY + 1),(((* Temp).BDAY + 2)); }' – Chaak 2011-05-14 17:17:45
我不確定我能否解碼日期的格式字符串。這究竟意味着什麼?如果輸入與預期完全不符,會發生什麼? – 2011-05-14 17:36:52