为什么我的指针分配导致程序崩溃

时间:2014-11-07 16:35:11

标签: c pointers

在编写基于相同基本原理的大型程序之前,我已经将下面的代码编写为一个小程序来测试。我昨天工作了,但今天早上它已经挂断了,我无法弄明白为什么。

代码挂断了: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;
}

1 个答案:

答案 0 :(得分:3)

在编译器中启用警告。我得到了:

../main.c:53:9: warning: assignment from incompatible pointer type [enabled by default]
../main.c:54:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘struct list *’ [-Wformat]
../main.c:57:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘struct list *’ [-Wformat]
../main.c:62:5: warning: too many arguments for format [-Wformat-extra-args]
../main.c:44:18: warning: unused variable ‘next_rec’ [-Wunused-variable]
../main.c:23:13: warning: unused variable ‘k’ [-Wunused-variable]
../main.c:23:10: warning: unused variable ‘j’ [-Wunused-variable]
../main.c:23:7: warning: unused variable ‘i’ [-Wunused-variable]
../main.c: In function ‘breakRemove’:
../main.c:93:10: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]

修复它们以获得良好的开端。 :)

例如,第一个警告来自

ptr = pAns;

然后这将是head。这就是你的程序崩溃的原因(可能是其中一个原因)。

你有

char *pAns;
struct list *ptr = NULL;

然后将其分配给另一个。这没有意义。你应该更努力地学习/学习,因为修复错误的答案很多。

另一个例子是:

if (pAns != '\0')
  ;                <-- suspicious semicolon, remove it
{
  count++;
  printf("\n%d.  You typed:\"%s\"", count, pAns);  //test for correct pAns pointer assignment
}

要在代码块中启用警告,您可以执行以下操作:

“在settings =&gt; configure plugins =&gt;编译器中选中”启用所有编译器警告“选项”

或者这样做:

enter image description here

我找到了here