#include<stdio.h>
main()
{ int x[3][5]={{1,2,10,4,5},{6,7,1,9,10},{11,12,13,14,15}};
printf("%d\n",x);
printf("%d\n",*x); }
Run Code Online (Sandbox Code Playgroud)
这里首先printf将打印第一个元素的地址.那么为什么第二个printf不会在地址x处输出值,即第一个值.要打印我需要写的值**x.
为什么我们不能使用双指针来表示二维数组?
arr[2][5] = {"hello","hai"};
**ptr = arr;
Run Code Online (Sandbox Code Playgroud)
这里为什么双指针(**ptr)在这个示例中不起作用?
可能重复:
数组名是C中的指针吗?
数组和指针在C和C++中的实现方式是否不同?我遇到过这个问题,因为在这两种情况下我们都从元素的起始地址访问元素.所以,他们之间应该有密切的关系.请解释它们之间的确切关系.谢谢.
我没有那么多使用C,最近我对2d数组初始化问题感到困惑.我需要调试某人的代码并坚持以下(她的原始代码):
const int location_num = 10000;
bool **location_matrix;
if (node_locations)
{
location_matrix = (bool **)malloc(location_num*sizeof(bool *));
if (!location_matrix)
{
cout<<"error 1 allocating location_matrix" << endl;
exit;
}
for (i=0; i<location_num; i++)
{
location_matrix[i] = (bool *) malloc(location_num*sizeof(bool ));
if (!location_matrix[i])
{
cout<<"error 2 allocating location_matrix" << endl;
exit;
}
for (j=0; j<location_num; j++)
location_matrix[i][j] = false;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为是多余的,所以我将其更改为以下内容:
location_matrix[location_num][location_num] = { {false} };
但是,分段错误在运行时发生.我的问题是:上面的代码是如何失败的?如果它看起来正确,动态分配和静态分配之间有什么区别?是因为维度可能不是常数,所以我们需要动态地做这个吗?另外,只是为了好奇,我如何malloc 2d数组存储指针?谢谢.
我有一个假设将2D数组作为参数的函数,我的代码看起来像这样 -
#include <stdio.h>
#include <stdlib.h>
void func(double**, int);
int main()
{
double m[3][3] = {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}};
func(m, 3);
}
void func(double **m, int dim)
{
int i, j ;
for(i = 0 ; i < dim ; i++)
{
for(j = 0 ; j < dim ; j++)
printf("%0.2f ", m[i][j]);
printf("\n");
}
}
Run Code Online (Sandbox Code Playgroud)
然后编译器说 -
test.c: In function ‘main’:
test.c:9:2: warning: passing argument 1 of ‘func’ from incompatible pointer type [enabled …Run Code Online (Sandbox Code Playgroud) 可能重复:
如何在C ++中使用数组?
2d数组是双指针吗?
二维数组和指针
我知道这是一个非常基本的问题,但是没有为我解决任何问题。这就是为什么要在这里发布它。在C ++中考虑声明int x[10];
这是一维数组,其中x是基本指针,它包含数组第一个元素的地址。因此,请x给我该地址并*x提供第一个要素。
同样的声明
int x[10][20];
Run Code Online (Sandbox Code Playgroud)
x这里是什么样的变量。当我做
int **z = x;
Run Code Online (Sandbox Code Playgroud)
编译器说它不能转换int (*)[20]为int **。为什么这样做cout<<x;并cout<<*x;给出相同的值?而且如果我将指针数组声明为
int *p[10];
Run Code Online (Sandbox Code Playgroud)
然后x和p(在其类型上)有区别吗?因为当一个声明int x[10]和int *p那么它是有效的分配x到p,但它不是那么二维数组的情况下?为什么?有人可以帮我清除这个问题,或者在此方面提供很好的参考资料。
我从C语言中学到的是int**matrix = matrix是指向int的指针,当我们想要创建一个矩阵时,我们将malloc一组contigus指针!所以这里的第一个指针指向一个指向int的指针,或者它可以指向一组指向int的指针(第一个指针当然指向第一个指针的地址)
Brievely指向1指针(只有一个)是否与从一组指针指向第一个指针相同?
我认为答案在于这个问题究竟什么是一系列的东西?
简单的一维数组被认为是一个指针,但矩阵的情况也是如此?
但是,立方体int[5][5][5]也会被认为是int ***?
我一直遇到分段错误,但我不确定这意味着什么或如何分辨它是什么原因(我对编程和C非常新).在main.c调用的这个函数中,我需要确定二维数组的eacg行中最小数字的索引.
这是我的代码:
#include "my.h"
void findfirstsmall (int x, int y, int** a)
{
int i;
int j;
int small;
small = y - 1;
printf("x = %3d, y = %3d\n", x, y); //trying to debug
printf("f. The first index of the smallest number is: \n");
for(i = 0; i < x; i++)
{
for(j = 0; j < y; i++) <---------- needs to be j, for any future readers
{
if(a[i][small] > a[i][j])
small = j;
printf("small = %3d\n", small); …Run Code Online (Sandbox Code Playgroud)