我有一个用Windows 10编码的程序,它可以完美运行。 在这种情况下(我将对此进行简化,因为这是程序失败的地方),它采用200个元素(结构)的简单链接列表,并将前100个元素的数据复制到100个元素的数组中(数组[100])。 该程序可在Windows上完美运行,但不会在Ubuntu上复制单个结构。 这两个系统的GCC编译器之间是否有任何差异会导致这种情况?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 100
typedef struct{
int id;
char title[100];
char director[100];
char genre[100];
int likes;
int number_of_voters;
float average_rating;
int year;
int cost;
char color[100];
float ratingW;
}Movie;
struct Node{
Movie movie;
struct Node *next;
};
//Simply linked list
typedef struct{
struct Node *head;
}List;
//Array
typedef struct{
Movie movies[SIZE];
int num; //number of elements
}Array;
void Initialize(List *l);
void PrintList(List l);
void InsertNode(List *l, Movie reg);
void PrintMovie(Movie mov);
void PrintArray(Array arr);
void FromListToArray(List l, Array *arr);
int main(){
List sls;
Array a;
Initialize(&sls);
PrintList(sls); //Prints 200 movies, and shows the message "Movies loaded in the list ::: 200"
FromListToArray(sls, &a);
PrintArray(a); //Doesn't print any movie, and shows the message "Movies loaded in the array ::: 0"
return 0;
}
//Initializes the list
void Initialize(List *l){
l->head = NULL;
}
void PrintList(List l){
struct Node *p;
p = l.head;
while(p != NULL)
{
PrintMovie(p->movie);
p = p->next;
}
}
//Inserts a node at the beginning of the list
void InsertNode(List *l, Movie reg){
struct Node *r;
r = (struct Node *) malloc (sizeof(struct Node));;
if (r == NULL){
printf("No memory!\n");
}
else{
r->movie.id = reg.id;
strcpy(r->movie.title, reg.title);
strcpy(r->movie.director, reg.director);
strcpy(r->movie.genre, reg.genre);
r->movie.likes = reg.likes;
r->movie.number_of_voters = reg.number_of_voters;
r->movie.average_rating = reg.average_rating;
r->movie.year = reg.year;
r->movie.cost = reg.cost;
strcpy(r->movie.color, reg.color);
r->movie.ratingW = reg.ratingW;
r->next = l->head;
l->head = r;
}
}
/*Copies the data from a txt file into a simply linked list.
Line 1 is the id of the first movie, line 2 the title... line 10 is the color.
Line 11 is the id of the second movie, and so on... This repeated 200 times (200 movies = 2000 lines)*/
void FromTxtToList(List *l, FILE *f){
int j = 0;
char cad[100];
Movie reg;
f = fopen("movies.txt", "r");
if (f == NULL){
printf("Error, could not open the file\n");
}
else{
while(!feof(f)){
fgets(cad, 100, f);
reg.id = atoi(cad);
fgets(cad, 100, f);
strcpy(reg.title, cad);
fgets(cad, 100, f);
strcpy(reg.director, cad);
fgets(cad, 100, f);
strcpy(reg.genre, cad);
fgets(cad, 100, f);
reg.likes = atoi(cad);
fgets(cad, 100, f);
reg.number_of_voters = atoi(cad);
fgets(cad, 100, f);
reg.average_rating = atof(cad);
fgets(cad, 100, f);
reg.year = atoi(cad);
fgets(cad, 100, f);
reg.cost = atoi(cad);
fgets(cad, 100, f);
strcpy(reg.color, cad);
reg.ratingW = 0;
InsertNode(l, reg);
j++;
}
}
fclose(f);
printf("Movies loaded in the list ::: %d\n", j);
}
void PrintMovie(Movie mov){
printf("///////////////////////////////////\n");
printf("Id: %d\n", mov.id);
printf("Title: %s", mov.title);
printf("Director: %s", mov.director);
printf("Genre: %s", mov.genre);
printf("Likes: %d\n", mov.likes);
printf("Number of voters: %d\n", mov.number_of_voters);
printf("Average rating: %.2f\n", mov.average_rating);
printf("Year: %d\n", mov.year);
printf("Cost: $%d\n", mov.cost);
printf("Color or BW: %s", mov.color);
printf("Rating: %.2f\n", mov.ratingW);
printf("///////////////////////////////////\n");
}
void PrintArray(Array arr){
int i;
printf("\nArray: \n");
for(i=0; i < arr.num; i++){
PrintMovie(arr.movies[i])
}
}
void FromListToArray(List l, Array *arr){
int i = 0;
arr->num = 0;
struct Node *p;
p = l.head;
while ((p != NULL) && (arr->num < SIZE)){
if (strcmp(p->movie.color, "Color\n")==0){
//If I find a "colored" movie (color = "Color")
//Copy the data into the array
arr->movies[i].id = p->movie.id;
strcpy(arr->movies[i].title, p->movie.title);
strcpy(arr->movies[i].director, p->movie.director);
strcpy(arr->movies[i].genre, p->movie.genre);
arr->movies[i].likes = p->movie.likes;
arr->movies[i].number_of_voters = p->movie.number_of_voters;
arr->movies[i].average_rating = p->movie.average_rating;
arr->movies[i].year = p->movie.year;
arr->movies[i].cost = p->movie.cost;
strcpy(arr->movies[i].color, p->movie.color);
arr->movies[i].ratingW = p->movie.ratingW;
arr->num++;
i++;
}
p = p->next;
}
printf("Movies loaded in the array ::: %d\n", arr->num);
}
答案 0 :(得分:0)