如何通过引用将二维字符数组传递给另一个函数

问题描述

我想通过引用将我的二维字符数组传递给另一个函数。我用我的 int ** 做了同样的事情,它工作得很好。但是当谈到 char ** 时,同样的方法被打破了。我尝试了太多东西。我几乎阅读了所有问题,但我看不到我的案例。

首先,我有一个设置为 NULL 的单字符 **,我通过引用将它传递给另一个函数

这是我的主要功能

int main(){
    int **recommendU2 = NULL;
    char **bookNames = NULL;
    
    readFromCsv(&recommendU2,&bookNames,&bookCount,&userCount);

    printf("\n user[2][3] = %d \n",recommendU2[2][3]); //this works 
    printf("\n bookNames[1] = %s \n,bookNames[1]); //this doesn't works
    
    return 0;
}

这是我的另一个函数,它从 csv 读取并处理这些数据。它是做它的目的没有和问题。它从 csv 读取并逐行读取数据并毫无问题地进行处理。但我无法访问我的 char ** 数据。但是我可以访问我的 int ** 数据。我也没发现有什么区别。

void readFromCsv(int ***user,char ***bookNames,int *bookCount,int *userCount){
    FILE * fpointer;
    int lineCount = 0;  
    int count;
    int sizes[3];  
    fpointer = fopen("recdataset.csv","r");
    char recommendationSheet[RECOMMENDATIONSIZE];

    while(fgets(recommendationSheet,RECOMMENDATIONSIZE,fpointer)){
        if(lineCount>1){
            //this part is not important for this question
        }
        else if(lineCount == 0){
            userArraysMemoryAllocate(recommendationSheet,user,nuser,bookNames,&sizes[0]);
            (*user)[2][3] = 4;
            //I can access this user data both within this function and main function
        }
        else if(lineCount == 1){
            //actually I send variables to another function which named getBookNames() but when I can't reach data I SUSPECT it and move it's code to here. But this doesn't make any different too.

            char *p = recommendationSheet;
            int j = 0;
            int i = 0;
            while(1) {
                char *p2 = strchr(p,';');
                if(p2 != NULL)
                    *p2 = '\0';
                if(i>0)
                    strcpy((*bookNames)[i],p);
                printf("\n Book Name is = %s \n",(*bookNames)[i]); //This line works read bookNames correctly
                i++;
                if(p2 == NULL)
                    break;
                p = p2 + 1;
            }
            printf("\nbookName = %s \n",(*bookNames)[2]); //this line doesn't work
        } 
        lineCount++;
    }
    fclose(fpointer);
}

我无法让它工作。我尝试了很多变体,但没有一个有效。如果您有任何解决方案或任何其他想法来完成这项工作,我将不胜感激。提前致谢。

注意:这个程序太长,放在这里。所以我只在这里复制了有问题的部分。这包括如此多的函数调用。所以如果你有不明白的地方请说出来,我也可以添加这部分代码

更新

我也向 readfromCsv 函数添加了内存分配函数。这是代码。我的问题仍然存在。我以为我修复了它,但它导致了异常行为。

void readFromCsv(int ***user,int *userCount,){
FILE * fpointer;
int lineCount = 0;  
int i = 0;
int j = 0;
int len;
int count;
int sizes[3];
fpointer = fopen("recdataset.csv","r");
char recommendationSheet[RECOMMENDATIONSIZE];

while(fgets(recommendationSheet,fpointer)){
    len = strlen(recommendationSheet);
    if( recommendationSheet[len-1] == '\n' )
        recommendationSheet[len-1] = 0;
    if(lineCount>1){
        //not important
    }
    else if(lineCount == 0){
        char *p = recommendationSheet;
        i = 0;
        j = 0;
        int row = 0;
        int col = 0;
        
        while(i<3) {
            char *p2 = strchr(p,';');
            if(p2 != NULL)
                *p2 = '\0';
            if(i==0){
                row = atoi(p);  
                col = row;          
                if((*bookNames = (char**)malloc(row * sizeof(char*))) == NULL){
                printf("\nMemory allocation problem \n");
                exit(0);
                }
                for(j = 0; j < row; ++j){
                    if(((*bookNames)[j] = (char*)malloc(50 * sizeof(char))) == NULL){
                        printf("\nMemory allocation problem \n");
                        exit(0);
                    }
                }
            }
            else if(i==1){
                row = atoi(p);
                if((*user = (int**)malloc(row * sizeof(int*))) == NULL){
                    printf("\nMemory allocation problem \n");
                    exit(0);
                }           
                for(j = 0; j < row; ++j){
                    if(((*user)[j] = (int*)malloc(col * sizeof(int))) == NULL){
                        printf("\nMemory allocation problem \n");
                        exit(0);
                    }
                }           
            }
            else if(i==2){
                row = atoi(p);
                if((*nuser = (int**)malloc(row * sizeof(int*))) == NULL){
                    printf("\nMemory allocation problem \n");
                    exit(0);
                }           
                for(j = 0; j < row; ++j){
                    if(((*nuser)[j] = (int*)malloc(col * sizeof(int))) == NULL){
                        printf("\nMemory allocation problem \n");
                        exit(0);
                    }
                }
            }
            sizes[i] = row;
            i++;
            if(p2 == NULL)
                break;
            p = p2 + 1;
        }
        (*user)[2][3] = 4; // this works still here and main function
    }
    else if(lineCount == 1){
        char *pMemory = recommendationSheet;
        j = 0;
        i = 0;
        while(1) {
            char *p2 = strchr(pMemory,';');
            if(p2 != NULL)
                *p2 = '\0';
            if(i>0)
                (*bookNames)[i] = pMemory;
            printf("\n %d. Book name is = %s \n",i,(*bookNames)[i]); // works perfectly
            i++;
            if(p2 == NULL)
                break;
            pMemory = p2 + 1;
        }
    } 
    lineCount++;
}

fclose(fpointer);
}

我仍然无法从主函数访问我的 char **。我添加了内存分配过程。我虽然修复了它,但问题再次发生而没有任何变化。我根本不明白这个问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)