【问题标题】:Two-dimensional string arrangement using pointers in CC中使用指针的二维字符串排列
【发布时间】:2020-01-23 19:03:38
【问题描述】:

我想使用指针创建一个二维字符串数组,因为这样我以后可以释放动态内存并使用相同的标识符(变量)创建一个更小或更大的数组。

我使用了代码:

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

unsigned long rows = 0;
unsigned long cols = 0;
unsigned long chrs = 30;

char ***mtx = NULL;

int rszmtx(char ***, unsigned long, unsigned long);

int main(int argc, char *argv[]) {
    rszmtx(mtx, 0, 0);

    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            strcpy(mtx[r][c], "[]");
        }
    }

    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            printf("%s ", mtx[r][c]);
        }
        printf("\n");
    }

    return 0;
}

int rszmtx(char ***matrix, unsigned long y, unsigned long x) {
    if (y <= 0) {
        printf("Enter the number of rows: ");
        scanf("%lu", &rows);
    } else {
        y = rows;
    }

    if (x <= 0) {
        printf("Enter the number of cols: ");
        scanf("%lu", &cols);
    } else {
        x = cols;
    }

    free(matrix);
    matrix = (char ***) malloc(rows * sizeof(char **));

    if (matrix == NULL)
        return 1;

    for (int r = 0; r < rows; r++) {
        matrix[r] = (char **) malloc(cols * sizeof(char *));

        for (int c = 0; c < cols; c++) {
            matrix[r][c] = (char *) malloc (chrs * sizeof(char));
        }
    }

    return 0;
}

在函数“main”中,我想调用函数“rszmtx”并为用户指定的“mtx”分配一个大小。然后我希望所有字符串都是“[]”并将它们打印出来。问题是我既不能分配也不能打印任何东西。在测试时,我注意到如果我将映射和/或打印放在“rszmtx”函数中,一切正常。为什么会这样,如何在“rszmtx”函数之外映射和打印“mtx”?

【问题讨论】:

  • 您会想回顾一下为什么成为Three Star Programmer 通常不是一种恭维。 malloc 的返回不用投,没必要。见:Do I cast the result of malloc?
  • @DavidC.Rankin 嗯,我明白了。这很有意义,但我才刚刚开始使用这个 C 和指针,现在以这种方式处理它对我来说更容易理解。但是当我处理得更好时我会改变这三颗星,谢谢你的建议。

标签: c arrays pointers char


【解决方案1】:

您必须通过引用传递指针。例如

int rszmtx(char ****mtx, unsigned long y, unsigned long x);

//…

rszmtx(&mtx, 0, 0);

否则函数会处理原始指针mtx的副本,并且在函数中更改副本不会影响原始指针;指针。

而且全局定义指针是没有意义的。

【讨论】:

    【解决方案2】:

    如果它是一个字符串矩阵,我不会像你的解决方案那样使用 ********* 指针,只创建这样的函数集:

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #include <stdio.h>
    
    #define SFUNC 1
    
    typedef struct
    {
        int malloced;
        char *string;
    }STRING_t;
    
    typedef struct
    {
        size_t xsize;
        size_t ysize;
        STRING_t strings[];
    }STR2D_t;
    
    
    STR2D_t *init(size_t xsize, size_t ysize)
    {
        STR2D_t *str2D = calloc(sizeof(str2D) + xsize * ysize * sizeof(str2D -> strings[0]), 1);
        str2D -> xsize = xsize;
        str2D -> ysize = ysize;
        return str2D;
    }
    
    char *setstr2D(STR2D_t *str2D, const char *str, size_t xpos, size_t ypos)
    {
        char *result = NULL;
        #if SFUNC
        if(str2D && str2D -> xsize > xpos && str2D -> ysize > ypos)
        #endif
        {
            STRING_t *strStruct = &str2D -> strings[xpos + ypos * str2D -> ysize];
            #if SFUNC
            if(strStruct -> malloced)
            {
                free(strStruct -> string);
                strStruct -> malloced = 0;
            }
            #endif
            strStruct ->  string = (char *)str;
            result = (char *)str;
        }
        return result;
    }
    
    char *strcpy2D(STR2D_t *str2D, const char *str, size_t xpos, size_t ypos)
    {
        char *result = NULL;
        #if SFUNC
        if(str2D && str2D -> xsize > xpos && str2D -> ysize > ypos)
        #endif
        {
            STRING_t *strStruct = &str2D -> strings[xpos + ypos * str2D -> ysize];
            if(strStruct -> string) strcpy(strStruct -> string, str);
            result = strStruct -> string;
        }
        return result;
    }
    
    char *strmalloc2D(STR2D_t *str2D, size_t size, size_t xpos, size_t ypos)
    {
        char *result = NULL;
        #if SFUNC
        if(str2D && str2D -> xsize > xpos && str2D -> ysize > ypos)
        #endif
        {
            STRING_t *strStruct = &str2D -> strings[xpos + ypos * str2D -> ysize];
            result = realloc(strStruct -> malloced ? strStruct -> string : NULL, size);
            if(result) 
            {
                strStruct -> string = result;
                strStruct -> malloced = 1;
            }
        }
        return result;
    }
    
    
    char *strdup2D(STR2D_t *str2D, const char *str, size_t xpos, size_t ypos)
    {
        char *result = strmalloc2D(str2D, strlen(str) + 1, xpos, ypos);
        if(result) result = strcpy2D(str2D, str, xpos, ypos);
        return result;
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多