这个问题的目的是提供一个关于如何在C中动态正确分配多维数组的参考.这是一个经常被误解的主题,即使在一些C编程书籍中也很难解释.因此,即使是经验丰富的C程序员也很难做到正确.
我从编程教师/书籍/教程中了解到,动态分配多维数组的正确方法是使用指针指针.
然而,SO上的几个高代表用户现在告诉我这是错误和不好的做法.他们说指针到指针不是数组,我实际上并没有分配数组,而且我的代码不必要地慢.
这就是我教我分配多维数组的方法:
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
int** arr_alloc (size_t x, size_t y)
{
int** pp = malloc(sizeof(*pp) * x);
assert(pp != NULL);
for(size_t i=0; i<x; i++)
{
pp[i] = malloc(sizeof(**pp) * y);
assert(pp[i] != NULL);
}
return pp;
}
int** arr_fill (int** pp, size_t x, size_t y)
{
for(size_t i=0; i<x; i++)
{
for(size_t j=0; j<y; j++)
{
pp[i][j] = (int)j + 1;
}
}
return pp;
}
void arr_print (int** pp, size_t x, size_t y) …Run Code Online (Sandbox Code Playgroud) c arrays dynamic-arrays dynamic-allocation variable-length-array
几天来,我一直在阅读"学习艰难的道路",但这是我想要真正理解的东西.作者Zed写道,这char **是一个"指向(指向char的指针)",并说这是必需的,因为我试图指向二维的东西.
这是在网页上写的内容
char*已经是"指向char的指针",所以这只是一个字符串.但是你需要2个级别,因为名称是2维的,这意味着你需要char**作为"指向(指向char的指针)"类型的指针.
这是否意味着我必须使用一个可以指向二维的变量,这就是为什么我需要两个**?
只是一点点跟进,这也适用于n维吗?
这是相关的代码
char *names[] = { "Alan", "Frank", "Mary", "John", "Lisa" };
char **cur_name = names;
Run Code Online (Sandbox Code Playgroud) 我想创建一个函数来分配(带malloc/ calloc)声明为双指针的矩阵.我理解双指针矩阵如何工作以及如何分配它malloc,但当我传递我的矩阵(声明main()并初始化NULL)时,我的程序崩溃了.我想错误与我的allocMatrix()功能有关,因为如果我在main所有工作中顺利分配矩阵.谢谢 :-)
主要:
#include <stdio.h>
#include <stdlib.h>
#include "Data.h"
#define ROW 5
#define COL 5
int main(void) {
int i,j, ret;
int nRow, nCol;
int **mat=NULL; // double pointer matrix
nRow = 0;
nCol = 0;
//Insert n of row and columns
printf("Insert n of rows and columns:\n");
scanf("%d %d", &nRow, &nCol);
//Functions to allocate matrix
ret=allocMatrix(mat, nRow, nCol);
printf("Return value: %d\n",ret);
/*
this code works …Run Code Online (Sandbox Code Playgroud) 在练习一些简单的数组和指针基础知识时,我遇到了一些我无法理解的事情:
在我能找到的所有书面资料中,他们说二维数组的名称实际上是二维指针,这意味着如果我写:
int a[3][4];
Run Code Online (Sandbox Code Playgroud)
并声明一个指针:
int **d2;
Run Code Online (Sandbox Code Playgroud)
它们都是同一类型,我可以安全地分配:
d2 = a;
Run Code Online (Sandbox Code Playgroud)
使指针指向第一行的开头。
但最令人惊讶的是,这不起作用,我的问题是——为什么?
我会为您复制代码和我收到的警告:
#include <stdio.h>
int main() {
int **d2;
int a[5][2] = { {1, 2}, {3, 4}, {5, 7}, {7, 8}, {9, 11} };
d2 = a;
while (d2 < a + 2) {
printf("%d", **d2);
d2++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到这个诊断:
warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
d=a;
^
ptr.c:11:9: warning: comparison of distinct pointer types lacks a cast
while(d<a+2)
Run Code Online (Sandbox Code Playgroud) 我已经有了几年的Python,C#和Java经验,我刚开始学习C语言.
我在教程中学到了,这char anything[]总是一个指针.(今天有人告诉我这是错的) - 我认为我的问题与此有关.不过,我正在尝试获取char数组的长度:
#include <stdio.h>
int get_string_length(char * string)
{
int length = 0;
while(string[length] != '\0')
{
char c = string[length];
length++;
}
return length;
}
int get_string_size(char * string)
{
return sizeof(string);
}
int main()
{
printf("%d\n", get_string_size("hello world")); // returns 8
printf("%d\n", get_string_length("hello world")); // returns 11
printf("%d\n", sizeof("hello world")); // returns 12 -> Okay, because of '\0'-char
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果:
8
11
12
那么,为什么我的get_string_size方法会返回8而不是12?(因为两者都只打电话sizeof())