【C语言】动态分配二维字符串数组

article/2025/10/31 12:28:56

动态分配一个二维字符串数组

在这里插入图片描述

(1) 分配可能不连续的内存

申请

char**pps8Output = (char **) malloc(n * sizeof(char *));

对于pps8Output而言,它获得了一块动态分配的连续内存,这块连续的内存可以放n个char *指针。
下面为每一行malloc一次内存,这样分配的内存就不一定是连续的。

for (int i = 0; i < n; i++) 
{pps8Output[i] = (char *) malloc(lens * sizeof(char));
}

释放

本着先malloc的内存,后free的原则进行释放。

    for (int i = 0; i < n; i++) {free(pps8Output[i]);}free(pps8Output);

该方法:
n行的字符串数组,每个都是malloc动态分配的,所以整个二维字符串数组的内存可能不连续。

完整demo:

#include<stdio.h>
#include<stdlib.h>int main() {int n = 3;int lens = 10;char**pps8Output = (char **) malloc(n * sizeof(char *));///< mallocfor (int i = 0; i < n; i++) {pps8Output[i] = (char *) malloc(lens * sizeof(char));printf("pps8Output[%d] ptr:%p \r\n", i, pps8Output[i]);}///< freefor (int i = 0; i < n; i++) {free(pps8Output[i]);}free(pps8Output);return 0;
}//deubg:
//pps8Output[0] ptr:0x5625f38782c0 
//pps8Output[1] ptr:0x5625f38792f0 
//pps8Output[2] ptr:0x5625f3879310 

(2) 分配连续的内存

申请

char **pps8Output = (char **) malloc(n * sizeof(char *));
对于pps8Output而言,它获得了一块动态分配的连续内存,这块连续的内存可以放n个char *指针。
下面一次性malloc好全部的内存,然后计算每行的起始地址,我们通过下标访问 pps8Output与赋值。

pps8Output[0] = (char *) malloc(n * lens * sizeof(char));for (int i = 1; i < n; i++)
{pps8Output[i] = pps8Output[0] + i * lens;
}

释放

由于是一次malloc出来的,所以只需要一次free即可。

 free(pps8Output);

该方法:一次性malloc全部所需的内存,该分配方式属于连续的内存。

完整demo:

#include<stdio.h>
#include<stdlib.h>int main() {int n = 3;int lens = 10;char**pps8Output = (char **) malloc(n * sizeof(char *));///< mallocpps8Output[0] = (char *) malloc(n * lens * sizeof(char));for (int i = 1; i < n; i++){pps8Output[i] = pps8Output[0] + i * lens;printf("pps8Output[%d] ptr:%p\r\n", i, pps8Output[i]);}///< freefree(pps8Output);return 0;
}//deubg:
//pps8Output[1] ptr:0x5591309b52ca
//pps8Output[2] ptr:0x5591309b52d4

(3) 将二维字符串数组看成一维字符串数组

申请

char *ps8Output = (char *) malloc(n * lens * sizeof(char));
将二维字符串数组看做一维字符串数组,开始时一次性malloc全部所需要的内存,通过手工计算索引的方式来分配内存地址。

for (int i = 0; i < n; i++) 
{for (int j = 0; j < lens; j++) {*(ps8Output + (i*lens) + j) = 'a'; ///< 以'a'为例进行赋值演示}
}

释放

由于是一次malloc出来的,所以只需要一次free即可。

 free(ps8Output );

该方法:
将二维字符串数组看做一维字符串数组。pps8Output 是一个字符串指针,ps8Output+1就是ps8Output的地址+1,每一行都有lens个元素,那么使用 lens*i表示跨过的元素个数。

完整demo:

#include<stdio.h>
#include<stdlib.h>int main() {int n = 3;int lens = 10;char *ps8Output = (char *) malloc(n * lens * sizeof(char));///< mallocfor (int i = 0; i < n; i++) {   for (int j = 0; j < lens; j++) {*(ps8Output + (i*lens) + j) = 'a';printf("%p \r\n", (ps8Output + (i*lens) + j));}}///< freefree(ps8Output);return 0;
}//debug:
//0x5560bb0fb2a0 
//0x5560bb0fb2a1 
//0x5560bb0fb2a2 
//0x5560bb0fb2a3 
//0x5560bb0fb2a4 
//0x5560bb0fb2a5 
//0x5560bb0fb2a6 
//0x5560bb0fb2a7 
//0x5560bb0fb2a8 
//0x5560bb0fb2a9 
//0x5560bb0fb2aa 
//0x5560bb0fb2ab 
//0x5560bb0fb2ac 
//0x5560bb0fb2ad 
//0x5560bb0fb2ae 
//0x5560bb0fb2af 
//0x5560bb0fb2b0 
//0x5560bb0fb2b1 
//0x5560bb0fb2b2 
//0x5560bb0fb2b3 
//0x5560bb0fb2b4 
//0x5560bb0fb2b5 
//0x5560bb0fb2b6 
//0x5560bb0fb2b7 
//0x5560bb0fb2b8 
//0x5560bb0fb2b9 
//0x5560bb0fb2ba 
//0x5560bb0fb2bb 
//0x5560bb0fb2bc 
//0x5560bb0fb2bd 

引经据典

http://www.ituring.com.cn/book/1147


http://chatgpt.dhexx.cn/article/5f96nOlJ.shtml

相关文章

字符串数组的三种形式

一、字符串数组 1.0 字符串数组的两种形式 第一种形式&#xff1a;二维字符数组 char arry[7][10] {"Monday","Tuesday","Wednsday","Thurday","Friday","Saturday","Sunday"};在栈上分配了70字节内…

干货丨什么是字符串数组

一、概念理解 1.C语言中没有字符串类型&#xff0c;用一片内存空间保存一串字符&#xff0c;这片空间称作字符数组。 2.以‘\0’结尾的字符数组被称为字符串数组。‘\0’是字符串结束的标志。 3.用双引号括起来的都是字符串。 二、初始化 char str[6] {h,e,l,l,o,\0};//字…

c语言之字符串数组

还是在写图的存储结构的时候&#xff0c;遇到了问题&#xff0c;就是如何在一个数组中存放字符串&#xff0c;我相信这个问题 对于面向对象的编程语言来说&#xff0c;轻而易举&#xff0c;比如对于Java来说,直接像下面就可以了&#xff1a; 但是c语言没有String这个类型&…

Android 自定义弹窗提示(AlertDialog)

Android 自定义弹窗提示 Java文件&#xff1a; private void showDialog() {AlertDialog.Builder builder new AlertDialog.Builder(this);//获取界面View view LayoutInflater.from(this).inflate(R.layout.alert_dialog, null);//将界面填充到AlertDiaLog容器并去除边框bui…

AlertDialog对话框详例

Android—3分钟你也能学会AlertDialog对话框 在Android开发中&#xff0c;少不了AlertDialog对话框的使用&#xff0c;也是至关重要的一个功能。AlertDialog的构造方法被声明为protected&#xff0c;所以我们不能使用new来创建AlertDialog对象。 目录 Android—3分钟你也能学…

Android - AlertDialog 对话框

Android - AlertDialog 对话框 使用步骤&#xff1a; ①创建一个AlertDialog.Builder对象②设置标题 内容 正面负面按键 编写点击事件③调用.show() 显示AlertDialog XML部分&#xff1a; <?xml version"1.0" encoding"utf-8"?> <LinearLay…

AlertDialog替代ProgressDialog方案

在Android API level 26及以后版本中&#xff0c;ProgressDialog被弃用&#xff0c;使用以下方法可以替代ProgressDialog。 首先&#xff0c;使用AlertDialog新建一个对象作为容器&#xff0c;在这里我们首先设置对话标题&#xff0c;然后取消然后关闭返回键退出对话框&#x…

改变AlertDialog默认背景

安卓4.0自带的AlertDialog太丑了有木有&#xff1f;黑色的背景很难看&#xff0c;今天实现的是怎么自定义style实现改变AlertDialog背景 首先在values/styles.xml文件中增加如下style <style name"AlertDialog" parent"android:Theme.DeviceDefault.Light.…

Android App开发超实用实例 | AlertDialog对话框

介绍AlertDialog对话框从简到繁的多种设计方案,从新角度理解UI交互设计。 AlertDialog对话框允许自定义弹出对话框的内容,实现比之前讲解的几种对话框更丰富的功能。 01、带默认按钮的AlertDialog 程序单击按钮后执行第17行代码,调用自定义方法showAlertDialog()。 第22~6…

Android中AlertDialog的使用

Android中AlertDialog的使用 简述默认样式单选弹出框多选弹出框列表对话框自定义弹出框完整代码 简述 AlertDialog可以在当前的界面上显示一个对话框&#xff0c;这个对话框是置顶于所有界面元素之上的&#xff0c;能够屏蔽掉其他控件的交互能力&#xff0c;因此AlertDialog一般…

android-如何关闭AlertDialog.Builder对话框

android-如何关闭AlertDialog.Builder对话框 前言 在实际写代码的时候&#xff0c;AlertDialog.Builder的使用算是比较频繁的&#xff0c;在对AlertDialog.Builder关闭的时候&#xff0c;我们会发现&#xff0c;AlertDialog.Builder对话框没有相对应的finish()或者dismiss()这…

Android 自定义AlertDialog

Android 自定义popuWindow 对于项目需求中&#xff0c;系统的弹出框不能满足我们的需求&#xff0c;就只能通过自定义布局来实现自己相对应的功能&#xff0c;这里总结一篇关于AlertDialog的 具体操作 这里把弹出框封装了一个方法&#xff0c;使用的时候&#xff0c;只需要在…

Android进阶之AlertDialog自定义

AlertDialog的自定义方式有很多种&#xff0c;这里介绍两种。 第一种是比较简单的&#xff0c;只自定义内容。 在AlertDialog使用详解中&#xff0c;非常详细的介绍了以下六种使用方法。 一、简单的AlertDialog&#xff08;只显示一段简单的信息&#xff0c;比如about us&…

Android入门第九篇之AlertDialog

本文来自http://blog.csdn.net/hellogv/ &#xff0c;引用必须注明出处&#xff01; 时隔一年&#xff0c;又要准备做Android的开发了&#xff0c;最近复习和整理一下Android的知识。这次要说的是AlertDialog&#xff0c;这种对话框会经常遇到。AlertDialog跟WIN32开发中的Dial…

对话框AlertDialog的基本使用(新手)

AlertDialog是点击某一组件时&#xff0c;本文用按钮举例&#xff0c;弹出一个对话框&#xff0c;供你选择接下来的操作。 基础用法 首先要给按钮设置点击事件&#xff0c;然后再onClick里构建 AlertDialog.Builder 方法 代码如下&#xff1a; findViewById(R.id.btn).setOn…

Android的 AlertDialog自定义布局与常用布局用法(弹窗)

1.直接上效果图&#xff0c;看看是不是你们想要的效果图 2.主活动MainActivity2的代码如下 import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.app…

AlertDialog详解

对话框介绍与示例 对话框在程序中不是必备的&#xff0c;但是用好对话框能对我们编写的应用增色不少。采用对话框可以大大增加应用的友好性。比较常用的背景是&#xff1a;用户登陆、网络正在下载、下载成功或者失败的提示&#xff0c;还有&#xff0c;比如&#xff1a;短信来了…

Android的AlertDialog详解

转自&#xff1a;http://www.2cto.com/kf/201205/131876.html 参考文章&#xff1a;http://www.cnblogs.com/jiezzy/archive/2012/09/20/2694917.html http://blog.csdn.net/lizzy115/article/details/6924416 AlertDialog的构造方法全部是Protected的&#xff0c;所以不能直接…

AndroidStudio中AlertDialog的四种用法

目录 1.默认样式 2.单选弹出框 3.多选弹出框 4.自定义弹出框 补充&#xff01;&#xff01; 1.默认样式 android.support.v7.app.AlertDialog.Builder builder new android.support.v7.app.AlertDialog.Builder(AlertDialogActivity.this); builder.setTitle("请回…

Android之AlertDialog的基础使用

坦白说&#xff0c;AlertDialog我在工作中用得并不多&#xff0c;因为AlertDialog的样式比较固定和呆板&#xff0c;为了和App的整体设计匹配&#xff0c;一般都是使用自定义的Dialog&#xff0c;只有在要求不高时用一下。但是作为Android的基础控件之一&#xff0c;掌握它是十…