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

标签 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;
}

最佳答案

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

../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
}
<小时/>

要在代码块中打开警告,您可以这样做:

“在设置中选中“启用所有编译器警告”选项=>配置插件=>编译器”

或者这样做:

enter image description here

我发现here .

关于c - 为什么我的指针分配会导致程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26805846/

相关文章:

C语言与数据结构

C 程序,如何使用循环在更大的形状上打印形状?

c - 使用 putchar 进行整数提升

c - 字符指针遇到问题

c - HTTP POST 到 google form C 程序

c - 如何为字符串数组创建指针?

C 指针概念

C 通过函数指针或不通过函数指针进行回调。为什么没有区别呢?

c++ - 了解递归函数中的数组和指针

Char 双向链表