在函数之间传递数组

时间:2014-10-25 19:20:51

标签: c arrays function parameters

我正在研究这个程序,将数字基数转换为新基数。主要是通过将第一个基数转换为10,然后将十个基数转换为新基数。实际的转换是有效的,当我将它传递给main时,它会正确读取。但是,当我将'charArray'传递给'findAnswer'函数时,它不会打印出值并且我的程序崩溃了。所以它没有正确传递我假设。有人看到我犯的错误吗? 输出:1101 [...然后程序崩溃]

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

char** buildAnsArray(FILE *ifp);

char* baseConversion(int num, int base, int newBase);

char* pow2Converter(int num, int base, int newBase);

int findAnswer(char **answerArray, char **charArray, int high, int low);

int main()
{
    int num, base, newBase;
    char **answerArray;
    char *answerBaseConversion;
    char *answerPowConversion;
    char charArray[10][10];
    int i = 0;
    FILE *ifp;
    ifp = fopen("data.txt", "r");

    answerArray = buildAnsArray(ifp);

    while(fscanf(ifp, "%d %d %d\n", &num, &base, &newBase)!=EOF){

        answerBaseConversion = baseConversion(num, base, newBase);//baseConversion(num, base,     newBase);

        strcpy(charArray[i],answerBaseConversion);
        i++;
    }

    findAnswer(answerArray, charArray, i-1, 0);

    free(answerArray);

    fclose(ifp);

    return 0;
}




char** buildAnsArray(FILE *ifp){
    char *answerPtr;
    int numConversions;
    int i = 0; int j = 0;
    char **answerArray;
    char answerString[10];

    fscanf(ifp, "%d\n", &numConversions);

    answerArray = malloc(numConversions * sizeof(char*));

    for (i = 0; i < numConversions; i++){
        answerArray[i] = malloc(10 * sizeof(char));
    }

    while(j < numConversions){
        fscanf(ifp, "%s\n", &answerString);
        strcpy(answerArray[j],answerString);
        j++;
    }

    return answerArray;
}



char* baseConversion(int num, int base, int newBase){
    if(base == 10){
        char a[2];

        switch(num%newBase){
            case 0:
                a[0] = '0';
                break;
            case 1:
                a[0] = '1';
                break;
            case 2:
                a[0] = '2';
                break;
            case 3:
                a[0] = '3';
                break;
            case 4:
                a[0] = '4';
                break;
            case 5:
                a[0] = '5';
                break;
            case 6:
                a[0] = '6';
                break;
            case 7:
                a[0] = '7';
                break;
            case 8:
                a[0] = '8';
                break;
            case 9:
                a[0] = '9';
                break;
            case 10:
                a[0] = 'A';
                break;
            case 11:
                a[0] = 'B';
                break;
            case 12:
                a[0] = 'C';
                break;
            case 13:
                a[0] = 'D';
                break;
            case 14:
                a[0] = 'E';
                break;
            case 15:
                a[0] = 'F';
                break;
        }

        a[1] = '\0';

        char *str;

        if (num/newBase != 0)
            str = baseConversion(num/newBase, base, newBase);

        else{
            str = malloc(11 * sizeof(char));
            str[0] = '\0';
        }

        strcat(str, a);

        return str;
    }

    else if(newBase==10){
        int num3;
        num3 = (((((((num/1000)*base)+((num%1000)/100))*base) + (((num%1000)%100)/10)) *base) +     (((num%1000)%100)%10));

    }

    else{
        char *str2 = baseConversion(num, base, 10);

        int num2 = atoi(str2);

        return baseConversion(num2, 10, newBase);
    }

}

int findAnswer(char **answerArray, char **charArray, int high, int low){
        printf("%s",answerArray[high]);
        printf("%s", charArray[low]);

    if(strcmp(answerArray[high], charArray[low])==0){
        printf("WOO");
        return low;//return to a string
    }

    else{
        if (low == high)
            return 0;//return to string? or recursion

        else{
            printf("hi");
            return findAnswer(answerArray, charArray, high, low+1);//instead of returnning to output, return to a string.
        }

    }
}

0 个答案:

没有答案