Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将一个链表指针指向另一个链表_C_Pointers_Linked List_Structure - Fatal编程技术网

C 将一个链表指针指向另一个链表

C 将一个链表指针指向另一个链表,c,pointers,linked-list,structure,C,Pointers,Linked List,Structure,我试图将一个结构中的指针指向另一个结构的节点。我已经被困在这10个小时了。有人能帮我修改代码吗?我在curr\u users->playlist=p\u playlists中遇到分段错误。我指错了吗 struct playlist_ { int album; int track_num; struct playlist_ *next; }; typedef struct playlist_ playlists; struct users_ { int user_ID; s

我试图将一个结构中的指针指向另一个结构的节点。我已经被困在这10个小时了。有人能帮我修改代码吗?我在
curr\u users->playlist=p\u 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;

错误似乎在这一行:

p_playlists = malloc(sizeof(playlists *));
您正在为指向
播放列表
结构的指针分配足够的内存,而没有为整个
播放列表
结构分配足够的内存。将行更改为:

p_playlists = malloc(sizeof(playlists));
为播放列表结构分配足够的内存

编辑

如下面的注释所示,您还需要在
else
块中为
curr\u用户
分配一些内容。然后,除非程序中有任何其他错误,否则它应该可以工作:)

错误似乎在这一行:

p_playlists = malloc(sizeof(playlists *));
您正在为指向
播放列表
结构的指针分配足够的内存,而没有为整个
播放列表
结构分配足够的内存。将行更改为:

p_playlists = malloc(sizeof(playlists));
为播放列表结构分配足够的内存

编辑

如下面的注释所示,您还需要在
else
块中为
curr\u用户
分配一些内容。然后,除非您的程序中出现任何其他错误,否则它应该可以工作:)

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

为了尽量减少混淆,确保正确操作,并在发生某些更改时尽量减少维护工作量,请始终像这样使用
malloc

type *pointer = malloc(count * sizeof(*pointer));
注意,在这种情况下,
指针的
类型
只提到一次。如果它改变了,你就不需要去碰代码的其余部分。另外,
sizeof(*pointer)
始终正确显示
pointer
中可能存在的元素的大小


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

users *head_users, *curr_users, *p_users, *users_pointer;
未初始化的,您正在检查

if( !(head_users))

??因为你的评论说如果列表是空的,那么创建第一个用户,我猜你需要的是使
的头用户成为全局用户,或者将其传递给
事务
,并在程序开始时将其初始化为
NULL

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

为了尽量减少混淆,确保正确操作,并在发生某些更改时尽量减少维护工作量,请始终像这样使用
malloc

type *pointer = malloc(count * sizeof(*pointer));
注意,在这种情况下,
指针的
类型
只提到一次。如果它改变了,你就不需要去碰代码的其余部分。另外,
sizeof(*pointer)
始终正确显示
pointer
中可能存在的元素的大小


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

users *head_users, *curr_users, *p_users, *users_pointer;
未初始化的,您正在检查

if( !(head_users))

??因为你的评论说如果列表为空,那么创建第一个用户,我猜你需要的是使
头用户成为全局用户,或者将其传递给
事务
,并在程序开始时将其初始化为
NULL

我有
p\u playlists=malloc(sizeof(playlists))
但是在我发布这个问题之前,我把它改成了实验。我把它改回来了,但它仍然给我同样的错误。你在程序中有另一个错误。如果程序进入
If(!(head_users))
条件,则您永远不会设置
curr_users
变量。通过调用
curr\u users->playlist=p\u playlists
您正在取消对错误变量的引用(
curr\u users
)。这将导致seg错误。如果(head_users==NULL){}
,我是否应该将其更改为
?另外,正如所指出的,您没有初始化指针变量。您应该始终将变量初始化为一些“安全”默认值;在指针的情况下,使用
NULL
(即
users*head\u users=NULL
)我将其初始化为NULL,只是忘记将其复制到问题中
但是在我发布这个问题之前,我把它改成了实验。我把它改回来了,但它仍然给我同样的错误。你在程序中有另一个错误。如果程序进入
If(!(head_users))
条件,则您永远不会设置
curr_users
变量。通过调用
curr\u users->playlist=p\u playlists
您正在取消对错误变量的引用(
curr\u users
)。这将导致seg错误。如果(head_users==NULL){}
,我是否应该将其更改为
?另外,正如所指出的,您没有初始化指针变量。您应该始终将变量初始化为一些“安全”默认值;对于指针,使用
NULL
(即
users*head\u users=NULL
)我已将其初始化为NULL,只是忘记将其复制到问题。我已初始化,但忘记在问题上复制它。然后,如果代码中缺少任何其他内容,把它们放在这里,因为错误可能就在那里。我确实初始化了,但忘了在问题上复制它。然后,如果代码中缺少任何东西,就把它们放在这里,因为错误可能就在那里。旁注:您可以这样定义结构:
typedef struct users\u{/*whatever*/}users在一条语句中同时执行定义和类型定义。此外,由于
struct-users
users
是两个不同的东西,因此您甚至可以删除下划线:
typedef-struct-users{/**/}users。更进一步,您甚至可以删除结构名:
typedef struct{/**}users虽然我不推荐最后一个。旁注:您可以这样定义结构:
typedef struct users{/*which*/}users两者都做