将整数传递给C中的函数 - 返回0

时间:2017-10-01 22:22:18

标签: c methods mergesort

我正在编写一个程序来合并一个我拥有的结构数组。

我正在尝试传递L和R的整数值,当我在mergesort函数中printf值时,值变为0。

这完全是我的代码。在mergesort的Sorter.c调用上发生此问题。

SORTER.C

#include "mergesort.h"
#include "Sorter.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char * argv[]){

    /*
    // checks if num of arguments is correct
    if (argc != 2){
        printf("Sorry, the input isn't in a correct format 1");
        return 1;
    }

    // checks if second argument is -c
    if (strcmp(argv[1],"-c") != 0){
        printf("Sorry, the input isn't in a correct format 2");
        return 1;
    }*/

    // stores third argument as the selected column to sort
    char selectedColumn[strlen(argv[2])];
    strcpy(selectedColumn,argv[2]);

    int numOfLines = 5043;
    //Generate this dynamically

    FILE *fp;
    fp = fopen("movie_metadata.csv","r");

    char buffer[1000];
    fgets(buffer, 1000, fp);

    //printf("The first character is: %s", line);

    Row *allRows = (Row*) malloc(sizeof(Row) * numOfLines);

    char * splitLine;
    char* lineCopy;

    int i;
    for (i = 0; i < numOfLines; i++){
        fgets(buffer, numOfLines, fp);
        lineCopy = buffer;

        splitLine = strtok (lineCopy,",");
        color = splitLine;

        splitLine = strtok (NULL, ",");
        director_name = splitLine;

        splitLine = strtok (NULL, ",");
        num_critic_for_reviews = splitLine;

        splitLine = strtok (NULL, ",");
        duration = splitLine;

        splitLine = strtok (NULL, ",");
        director_facebook_likes = splitLine;

        splitLine = strtok (NULL, ",");
        actor_3_facebook_likes = splitLine;

        splitLine = strtok (NULL, ",");
        actor_2_name = splitLine;

        splitLine = strtok (NULL, ",");
        actor_1_facebook_likes = splitLine;

        splitLine = strtok (NULL, ",");
        gross = splitLine;

        splitLine = strtok (NULL, ",");
        genres = splitLine;

        splitLine = strtok (NULL, ",");
        actor_1_name = splitLine;

        splitLine = strtok (NULL, ",");
        movie_title = splitLine;

        splitLine = strtok (NULL, ",");
        num_voted_users = splitLine;

        splitLine = strtok (NULL, ",");
        cast_total_facebook_likes = splitLine;

        splitLine = strtok (NULL, ",");
        actor_3_name = splitLine;

        splitLine = strtok (NULL, ",");
        facenumber_in_poster = splitLine;

        splitLine = strtok (NULL, ",");
        plot_keywords = splitLine;

        splitLine = strtok (NULL, ",");
        movie_imdb_link = splitLine;

        splitLine = strtok (NULL, ",");
        num_user_for_reviews = splitLine;

        splitLine = strtok (NULL, ",");
        language = splitLine;

        splitLine = strtok (NULL, ",");
        country = splitLine;

        splitLine = strtok (NULL, ",");
        content_rating = splitLine;

        splitLine = strtok (NULL, ",");
        budget = splitLine;

        splitLine = strtok (NULL, ",");
        title_year = splitLine;

        splitLine = strtok (NULL, ",");
        actor_2_facebook_likes = splitLine;

        splitLine = strtok (NULL, ",");
        imdb_score = splitLine;

        splitLine = strtok (NULL, ",");
        aspect_ratio = splitLine;

        splitLine = strtok (NULL, ",");
        movie_facebook_likes = splitLine;

        splitLine = strtok (NULL, ",");

        Row row = { .color = color, .director_name = director_name, .num_critic_for_reviews = num_critic_for_reviews, .duration = duration, .director_facebook_likes = director_facebook_likes, .actor_3_facebook_likes = actor_3_facebook_likes,
            .actor_2_name = actor_2_name, .actor_1_facebook_likes = actor_1_facebook_likes, .gross = gross, .genres = genres, .actor_1_name = actor_1_name, .movie_title = movie_title, .num_voted_users = num_voted_users,
            .cast_total_facebook_likes = cast_total_facebook_likes, .actor_3_name = actor_3_name, .facenumber_in_poster = facenumber_in_poster, .plot_keywords = plot_keywords, .movie_imdb_link = movie_imdb_link,
            .num_user_for_reviews = num_user_for_reviews, .language = language, .country = country, .content_rating = content_rating, .budget = budget, .title_year = title_year, .actor_2_facebook_likes = actor_2_facebook_likes,
            .imdb_score = imdb_score, .aspect_ratio = aspect_ratio, .movie_facebook_likes = movie_facebook_likes
        };

        allRows[i] = row;

        //printf("%d - Director: %s and %d\n",i, *allRows[i].director_name, i);
    }
    printf("Director: %s\n", *allRows[0].director_name);
    /*int test = 5042;
    int* testptr = &test;
    printf("%d", *testptr);

    int test2 = 5042;
    int* test2ptr = &test2;
    printf("%d", *test2ptr);*/

    int test = 0;
    int test2 = 5042;
    printf("this: %d", test2);

    mergeSort(*allRows, test, test2, "director_name");

    printf("Director: %s\n", *allRows[0].director_name);



    return 0;
}

SORTER.H

/*
 * Sorter.h
 *
 *  Created on: Sep 27, 2017
 *      Author: Peter and Umar
 */

#ifndef SORTER_H_
#define SORTER_H_



typedef struct Row {

    char* color[100];
    char* director_name[100];
    int* num_critic_for_reviews;
    int* duration;
    int* director_facebook_likes;
    int* actor_3_facebook_likes;
    char* actor_2_name[100];
    int* actor_1_facebook_likes;
    int* gross;
    char* genres[500];
    char* actor_1_name[100];
    char* movie_title[100];
    int* num_voted_users;
    int* cast_total_facebook_likes;
    char* actor_3_name[100];
    int* facenumber_in_poster;
    char* plot_keywords[1500];
    char* movie_imdb_link[1000];
    int* num_user_for_reviews;
    char* language[40];
    char* country[40];
    char* content_rating[100];
    int* budget;
    int* title_year;
    int* actor_2_facebook_likes;
    int* imdb_score;
    int* aspect_ratio;
    int* movie_facebook_likes;



} Row;

char* color;
char* director_name;
char* actor_2_name;
char* genres;
char* actor_1_name;
char* movie_title;
char* actor_3_name;
char* plot_keywords;
char* movie_imdb_link;
char* language;
char* country;
char* content_rating;

int* num_critic_for_reviews;
int* duration;
int* director_facebook_likes;
int* actor_3_facebook_likes;
int* actor_1_facebook_likes;
int* gross;
int* num_voted_users;
int* cast_total_facebook_likes;
int* facenumber_in_poster;
int* num_user_for_reviews;
int* budget;
int* title_year;
int* actor_2_facebook_likes;
int* imdb_score;
int* aspect_ratio;
int* movie_facebook_likes;

#endif

MERGESORT.C

#include "mergesort.h"
#include "Sorter.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>



// Merges two subarrays of arr[]
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(Row* arr[], int l, int m, int r, char column[]){
    int i, j, k;
    int n1 = m - l + 1;
    int n2 =  r - m;

    /* create temp arrays */
    Row* L[n1];
    Row* R[n2];

    /* Copy data to temp arrays L[] and R[] */
    for (i = 0; i < n1; i++)
        *L[i] = *arr[l + i];
    for (j = 0; j < n2; j++)
        *R[j] = *arr[m + 1+ j];

    /* Merge the temp arrays back into arr[l..r]*/
    i = 0; // Initial index of first subarray
    j = 0; // Initial index of second subarray
    k = l; // Initial index of merged subarray
    printf("right before 1\n");
    while (i < n1 && j < n2)
    {

        //strcmp(column,"director_name") ==0
        //strcmp(column,"actor_2_name") ==0
        //strcmp(column,"genres") ==0
        //strcmp(column,"actor_1_name") ==0
        //strcmp(column,"movie_title") ==0
        //strcmp(column,"actor_3_name") ==0
        //strcmp(column,"plot_keywords") ==0
        //strcmp(column,"movie_imdb_link") ==0
        //strcmp(column,"language") ==0
        //strcmp(column,"country") ==0
        //strcmp(column,"content_rating") ==0

        // do this for all the different column names ^^^
        printf("right before\n");
        if (strcmp(column,"director_name") ==0)
        {
            printf("equals director name\n");
            if (strcmp(L[i]->director_name,R[j]->director_name) <= 0){
                printf("next level\n");
                *arr[k] = *L[i];
                i++;
            } else {
                *arr[k] = *R[j];
                j++;
            }
            k++;
        }

//        if (L[i] <= R[j])
//        {
//            arr[k] = L[i];
//            i++;
//        }
//        else
//        {
//            arr[k] = R[j];
//            j++;
//        }
//        k++;
    }

    /* Copy the remaining elements of L[], if there
       are any */
    while (i < n1)
    {
        *arr[k] = *L[i];
        i++;
        k++;
    }

    /* Copy the remaining elements of R[], if there
       are any */
    while (j < n2)
    {
        *arr[k] = *R[j];
        j++;
        k++;
    }
}

/* l is for left index and r is right index of the
   sub-array of arr to be sorted */
void mergeSort(Row* arr[], int l, int r, char column[]){
    printf("%d\n", r);
printf("called\n");
printf("%d\n", l);
printf("%d\n", r);

//THIS IS PRINTING 0 FOR SOME REASON ^ need to debug

//printf("%s\n", *arr[0]->director_name);
//printf("%s\n", column);




    if (l < r){
        printf("l < r\n");
        // Same as (l+r)/2, but avoids overflow for
        // large l and h
        int m = l+(r-l)/2;

        // Sort first and second halves
        mergeSort(arr, l, m, column);
        mergeSort(arr, m+1, r, column);

        merge(arr, l, m, r, column);
    }

    printf("Merged!\n");
}

我传入的值变为零。

我尝试过创建指针并传递它们,但值仍为零!我在这里吹脑筋......这是一项我正在努力的简单任务。我很感激任何帮助,我尝试了各种在线解决方案,但显然我在这里遗漏了一些东西..

谢谢

0 个答案:

没有答案