【问题标题】:Pointer one linked list to another linked list将一个链表指向另一个链表
【发布时间】:2012-04-03 22:58:28
【问题描述】:

我试图将一个结构中的指针指向另一个结构的节点。我已经被困在这10个小时了。有人可以帮我修复我的代码吗?我在curr_users -> playlist = p_playlists; 遇到分段错误。我是不是指错了?

struct playlist_ {
  int album;
  int track_num;
  struct playlist_ *next;
};
typedef struct playlist_  playlists;

struct users_ {
  int user_ID;
  struct playlist_ *playlist;
  struct users_ *next;
};
typedef struct users_ users;

int transaction(FILE *transaction_file,album *all_album){
  int transaction_id,i;
  int album_ID,
      account_number,
      add_playlist_user,
      add_playlist_album,
      add_playlist_track;

  users *head_users,*curr_users,*p_users,*users_pointer;
  playlists *head_playlists,*curr_playlists,*p_playlists,*playlist_pointer;

  head_users = NULL;

  fscanf(transaction_file,"%d\n",&account_number);

  /*Checks for empty list, if true creates the first user*/
  if( !(head_users)){
    p_users = malloc(sizeof(users ));
    p_users -> user_ID = account_number;
    head_users = p_users;
    head_users -> next = NULL;
    users_pointer = head_users;

  /*If list is not empty create new user and puts it in front of list*/
  }else{
    p_users = malloc(sizeof(users));
    p_users -> user_ID = account_number;
    curr_users = p_users;
    curr_users -> next = head_users;
    head_users = curr_users;
    users_pointer = head_users;
    }
  /*Create an empty playlist for user and set everything to null*/

  p_playlists = malloc(sizeof(playlists *));
  curr_playlists = p_playlists;
  curr_playlists -> album = 5;
  curr_playlists -> track_num = 5;
  curr_playlists -> next = NULL;
  curr_users -> playlist = p_playlists; 

运行此代码时收到的错误消息:

Program received signal SIGSEGV, Segmentation fault.
0x00011050 in transaction (transaction_file=0xff3675cc, all_album=0x226b0)
    at functions.c:94
94            curr_users -> playlist = p_playlists;

【问题讨论】:

  • 旁注:您可以像这样定义结构:typedef struct users_ { /* whatever */ } users; 在一个语句中同时进行定义和 typedef。此外,由于struct usersusers 是两个不同的东西,你甚至可以去掉下划线:typedef struct users { /* */ } users;。更重要的是,您甚至可以删除结构名称:typedef struct { /* */ } users;,尽管我不推荐最后一个。

标签: c pointers linked-list structure


【解决方案1】:

人们已经给出了答案,但我想我会通过一个建议使其更完整:

为了最大程度地减少混乱,确保您做对了,并在发生某些更改时尽量减少维护工作,请始终使用malloc,如下所示:

type *pointer = malloc(count * sizeof(*pointer));

请注意,在这种情况下,pointertype 仅被提及一次。如果它发生变化,您无需接触其余代码。此外,sizeof(*pointer) 始终正确显示pointer 中可以存在的元素的大小。


现在回到您的代码,您是否注意到您有以下局部变量:

users *head_users, *curr_users, *p_users, *users_pointer;

那些没有被初始化,你正在检查

if( !(head_users))

?由于您的评论说if list is empty, create the first user,我猜您需要将head_users 设为全局,或将其传递给transaction 并在程序启动时将其初始化为NULL

【讨论】:

  • 我确实初始化了,但忘了在问题上复制它。
  • 那么如果代码中还有其他缺失的地方,把它们放在这里,因为错误可能在那里。
【解决方案2】:

错误似乎在这一行:

p_playlists = malloc(sizeof(playlists *));

您为指向playlist_ 结构的指针分配了足够的内存,而没有为整个playlist_ 结构分配足够的内存。将行更改为:

p_playlists = malloc(sizeof(playlists));

playlist_ 结构分配足够的内存。

编辑
如下面的 cmets 所示,您还需要在 else 块中为 curr_users 分配一些东西。然后,除非您的程序中出现任何其他错误,否则它应该可以工作:)

【讨论】:

  • 我有p_playlists = malloc(sizeof(playlists));,但在发布此问题之前我将其更改为实验。我改回来了,但它仍然给我同样的错误。
  • 程序中还有另一个错误。如果程序进入if(!(head_users)) 条件,那么您永远不会设置curr_users 变量。通过调用curr_users->playlist = p_playlists;,您正在解除对错误变量(curr_users)的引用。这将导致段错误。
  • 我应该把它改成if( head_users == NULL){}吗?
  • 另外,正如Shahbaz 指出的那样,您没有初始化指针变量。您应该始终将变量初始化为一些“安全”的默认值;在指针的情况下,使用NULL(即users* head_users = NULL
  • 我确实将其初始化为 NULL,只是忘记将其复制到问题中。
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多