zoukankan      html  css  js  c++  java
  • c 二维数组动态分配和释放

    c动态语言

    函数声明的头文件在<stdlib.h>

     使用malloc函数为字符串分配内存 --》记得释放内存 free()

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    char *concat(const char *s1, const char *s2);
    
    int main(void) {
        char *p;
        p = concat("abc", "def");
        printf("%s
    ", p); //abcdef
        return 0;
    }
    
    char *concat(const char *s1, const char *s2) {
    
        char *result;
    
        result = malloc(strlen(s1) + strlen(s2) + 1);
        if (result == NULL) {
            printf("Error: malloc failed in concat
    ");
            exit(EXIT_FAILURE);
        }
        strcpy(result, s1);
        strcat(result, s2); //放数据
        return result;
    }

     利用动态内存,字符串数组

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_REMIND 50
    #define MSG_LEN 60
    
    int read_line(char str[], int n);
    
    int main() {
        char *reminders[MAX_REMIND]; //定义字符串数组
        char day_str[3], msg_str[MSG_LEN + 1]; // day_str是为了转换日期用,msg_str是存储字符串,存进数组用
        int day, i, j, num_remind = 0;
    
        for (;;) {
            if (num_remind == MAX_REMIND) {
                printf("-- No space left --
    ");
                break;
            }
            printf("Enter day and reminder: ");
            scanf("%2d", &day);
            if (day == 0)
                break;
            sprintf(day_str, "%2d", day); //把day转换成字符串
            read_line(msg_str, MSG_LEN); //输入字符,变成字符串
    
    /*        for (i = 0; i < num_remind; i++) { // num_remind记录了有多少个字符串,这个步骤校验有问题,应该放到下面去
                printf("ii: %d
    ",i);
    
                if (strcmp(day_str, reminders[i]) < 0)
                    break;
            }
            for (j = num_remind; j < i; j++)
                reminders[j] = reminders[j - 1];*/
            
            reminders[i] = malloc(2 + strlen(msg_str) + 1); //创建动态内存
            if (reminders[i] == NULL) {
                printf("-- No space left --
    ");
                break;
            }
    //        printf("i: %d
    ", i);
            strcpy(reminders[i], day_str);
            strcat(reminders[i], msg_str); // 放数据
            i++;
            num_remind++;
        }
        printf("
    Day Reminder 
    ");
        for (i = 0; i < num_remind; i++) {
            printf(" %s
    ", reminders[i]);
        }
        return 0;
    }
    
    int read_line(char str[], int n) {
        char ch;
        int i = 0;
        while ((ch = getchar()) != '
    ')
            if (i < n)
                str[i++] = ch;
        str[i] = '';
        return i;
    }

    一、 已知第二维

    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_REMIND 50
    #define MSG_LEN 60
    
    
    int main() {
        char (*a)[MSG_LEN + 1];
        a = (char (*)[MSG_LEN]) malloc(sizeof(char *) * MAX_REMIND);
        strcpy(a[0], "abcd");
        printf("%s
    ", a[0]); //abcd;
        free(a);
        a = NULL; //防止野指针
        return 0;
    }
    
    // 优化后的写法
    int main() {
    
        char (*a)[MSG_LEN + 1] = malloc(MSG_LEN + 1);
        if (a == NULL) {
            printf("malloc error");
            exit(1);
        }
    
        strcpy(a, "abcd");
        printf("a = %s
    ", a);
    
        free(a);
        a = NULL;
        return 0;
    }

    二、 已知第二维

    三、 已知第一维, 一次分配内存(保证内存的连续性)

    四、两维都未知

     五、两维都未知, 一次分配内存(保证内存的连续性)

     

     注意:静态二维数组作为函数参数传递

    如果采用上述几种方法动态分配二维数组,那么将对应的数据类型作为函数参数就可以了。这里讨论静态二维数组作为函数参数传递,即按照以下的调用方式:

    int a[2][3];
    func(a);

    C语言中将静态二维数组作为参数传递比较麻烦,一般需要指明第二维的长度,如果不给定第二维长度,则只能先将其作为一维指针传递,然后利用二维数组的线性存储特性,在函数体内转化为对指定元素的访问。
    首先写好测试代码,以验证参数传递的正确性:
    (1)给定第二维长度

    Code-11
    void func(int a[][N])
    {
    printf("%d
    ", a[1][2]);
    } 

    (2)不给定第二维长度

    Code-12
    void func(int* a)
    {
    printf("%d
    ", a[1 * N + 2]);//计算元素位置
    } 

    注意:使用该函数时需要将二维数组首地址强制转换为一维指针,即func((int*)a);

  • 相关阅读:
    Django Admin 管理工具
    老男孩培训机构老师的博客
    pycharm版本选择并安装
    Linux命令-自动挂载文件/etc/fstab功能详解
    django urls路由匹配分发
    django templates模板
    Django models模型
    django views视图函数
    JDK与JRE、JVM三者间的关系及JDK的安装部署
    django 第一个项目测试
  • 原文地址:https://www.cnblogs.com/renfanzi/p/9145433.html
Copyright © 2011-2022 走看看