동일한 기본 원칙에 따라 더 큰 프로그램을 작성하기 전에 테스트하기 위해 아래 코드를 작은 프로그램으로 작성했습니다. 나는 어제 일하고 있었지만, 오늘 아침에 끊어지고있다. 왜 그런지 알 수 없다.내 포인터 할당으로 인해 프로그램이 중단되는 이유
코드가 끊기고 있습니다. new_rec-> next = head;
아래 코드를 살펴보고 도움 주셔서 감사합니다.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
FILE *fptr;
struct list {
char string[256];
char *ptr;
struct list *next;
};
unsigned int breakRemove(char string1[]);
int main(void)
{
if ((fptr = fopen("C:\\Users\\mgreene\\Documents\\EmailTemplates\\TestList.txt", "w")) == NULL)
{
fprintf(stderr, "Error opening file.");
exit(1);
}
int i, j, k, count=0;
char ans[256];
char *pAns;
pAns = ans;
struct list *ptr = NULL;
do
{
puts("\nEnter some text: ");
fgets(ans, 256, stdin);
breakRemove(ans);
if (pAns != '\0');
{
count++;
printf("\n%d. You typed:\"%s\"", count, pAns); //test for correct pAns pointer assignment
}
struct list list1;
struct list *head = NULL;
struct list *new_rec = NULL;
struct list *curr_rec = NULL;
struct list *next_rec = NULL;
new_rec = (struct list*)malloc(sizeof(struct list));
if (!new_rec)
{
puts("\nMemory Allocation Error");
exit(1);
}
puts("\nFirst Memory Allocation Successful."); //acknowledge successful memory allocation
ptr = pAns;
printf("\nptr = %s", ptr); //test for pointer assignment
printf("\npAns = %s", pAns); //test for pointer assignment
head = ptr;
printf("\nhead = %s", head);// test for pointer assignment
printf("\nProblem is new_rec->next=head."); //test to isolate problem.
new_rec->next = head;
printf("\nnew_rec->next = ", new_rec->next);
head = new_rec;
curr_rec = head;
while (curr_rec->next != NULL)
{
curr_rec = curr_rec->next;
}
puts("\nList Pointer Memory Allocation Successful.");
curr_rec->next = new_rec;
new_rec->next = NULL;
strcpy(new_rec->string, ans);
printf("\n%s", curr_rec->string);
if (list1.string != '\0')
{
fprintf(fptr, "\n%d. %s", count, curr_rec->string);
}
}while (*pAns != '\0');
}
unsigned int breakRemove(char string1[]) //Function for removing line breaks from fgets.
{
unsigned int lenString;
lenString = strlen(string1);
if (string1[lenString-1]=='\n')
{
string1[lenString-1]='\0';
}
return (unsigned char)string1;
}
위로 올려!? 해당 특정 줄에서 실행이 중지됩니까? 브레이크 포인트를 설정 한 것처럼? –
디버거를 사용하여 문제의 범위를 좁히고 _minimal_ 재생 예제 만 게시하십시오. 내가해야 할 모든 경고를 고쳐서 시작하겠습니다. 그리고 아무 것도 얻지 못한다면 경고 수준을 최대로 설정하십시오. – Mat
두 가지 경고가 나타납니다. 하나는 ptr = pAns이고 다른 하나는 내 breakremove 함수입니다. 그게 내가받는 유일한 경고 야. 내가 휴식을 넣은 것처럼 멈추지는 않습니다, 그것은 이전에 언급 한 줄에 매달려 있습니다. – Matt