为什么在功能之外无法访问?

时间:2012-03-31 21:40:13

标签: c

当我尝试在函数外部访问all_tracks_ptr[1].tracks[1].tracks_title时,我不知道为什么我会出现分段错误。这只是我总代码的一小部分。我该如何解决?或者什么是将我从文件中扫描的信息返回到主文件的更好方法?

album *pharse_album(FILE *album_file,int *number_of_albums){
    int number_of_album,number_of_tracks,number_of_charaters;
    int i,j;
    char dummy_space;

    fscanf(album_file,"%d", &number_of_album);

    *number_of_albums = number_of_album;

    album *all_albums_p = (album *)malloc(sizeof(album)*number_of_album);
    for(j=0;j<number_of_album;j++){
        fscanf(album_file,"%d", &all_albums_p[j].num_tracks);
            tracks *all_tracks_p = (tracks*)malloc(sizeof(tracks)*all_albums_p[j].num_tracks);
        for(i=0;i<all_albums_p[j].num_tracks;i++){
            fscanf(album_file,"%d", &number_of_charaters);
            all_albums_p[j].tracks = (tracks *)malloc(sizeof(tracks)*all_albums_p[j].num_tracks);
            all_albums_p[j].tracks[i].tracks_title = (char *)malloc(sizeof(char)*(number_of_charaters+6));
            fscanf(album_file," ",dummy_space);
            fscanf(album_file, "%[^\n]s", all_albums_p[j].tracks[i].tracks_title);

            printf("%s\n",all_albums_p[j].tracks[i].tracks_title);
        }
      }
      return all_albums_p;
    }


    main(){

        all_tracks_ptr = pharse_album(album_file,number_of_albums);

        for(i=0; i < *number_of_albums ;i++){
            printf("%d : %d\n",i,all_tracks_ptr[i].num_tracks);
            printf("%s",all_tracks_ptr[1].tracks[1].tracks_title);

        }
    }

1 个答案:

答案 0 :(得分:0)

据我所知,代码永远不会产生struct tracks_,我认为这是all_tracks_ptr应指向的内容?