什么是错传递一个二维数组到相应的指针参数? [英] What is wrong passing a 2D array to a respective pointer argument?

查看:143
本文介绍了什么是错传递一个二维数组到相应的指针参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一些矩阵计算C语言为大学那天,我有一个5x5的矩阵开始,所以我硬codeD入来源。这就像双打的二维数组:

I've been doing some matrix calculation in C for university the other day where I had a 5x5 matrix to begin with so I hard-coded it into the source. It was a 2D array of doubles like:

/**
 * This is the probability-matrix for reaching from any profile
 * to another by randomly selecting a friend from the friendlist.
 */
static const double F[5][5] = {
  /*           P    , F    , L    , A    , S    */
  /* Peter */ {0    , 0.5  , 0.5  , 0    , 0    },
  /* Franz */ {1.0  , 0    , 0    , 0    , 0    },
  /* Lisa  */ {0    , 1/3.0, 0    , 1/3.0, 1/3.0},
  /* Anna  */ {0    , 0    , 0    , 0    , 1    },
  /* Sepp  */ {0    , 0.5  , 0.5  , 0    , 0    }
};

我想我的功能不被固定在5×5的矩阵操作,所以我总是行和/或COLS数传递给函数。这迫使我不要使用双击[] [X] 语法,因为它不能完全变量,转而使用双* 作为函数参数。

I wanted my functions to not be fixed to operate on 5-by-5 matrices so I always pass the number of rows and/or cols to the function. This forces me not to use the double [][X] syntax because it can not be completely "variable" and instead use a double* as function parameter.

inline size_t matrix_get(int rows, int i, int j);

inline void matrix_print(double* m, int rows, int cols);

inline void matrix_copy(double* d, const double* s, int rows, int cols);

void matrix_multiply(
  int m, int n, int l,
  const double* a, const double* b, double* d);

但调用接受一个函数的时候,我总是得到这样的警告双* 当我通过双击[5] [5] 代替。

fuenf_freunde.c:138:17: warning: incompatible pointer types passing 'double [5][5]' to parameter of
      type 'double *' [-Wincompatible-pointer-types]
  matrix_print( R, 5, 5);
                ^
fuenf_freunde.c:54:27: note: passing argument to parameter 'm' here
void matrix_print(double* m, int rows, int cols)
                          ^

使用铸造(双*)的F 解决了警告。

现在我的问题是


  • 我错了铸造一个二维double数组到双指针?

  • 为什么它的工作,如果其非法的?

  • 什么是n维任意大小的数组传递给函数的正确方法?

编辑:这清除了很多为我:<一href=\"http://stackoverflow.com/questions/25303647/accesing-a-2d-array-using-a-single-pointer\">Accesing使用单一指针一个二维数组

This cleared a lot up for me: Accesing a 2D array using a single pointer

所以,我应该只使用双击[X * Y] 而不是双击[X] [Y] 我认为。但它是合法的投双[] 双*

So I should just use double[x*y] instead of double[x][y] I think. But is it legal to cast double[] to double*?

推荐答案

我主张有没有错你做什么。您code只是缺少一个明确的投这将摆脱警告,说明你的任务是故意的。

I contend that there is nothing wrong with what you do. Your code just lacks an explicit cast which would get rid of the warning, indicating that your assignment is intentional.

可以通过指针访问的一维数组的元素,并且递归地应用,它遵循n维矩阵可以通过指针到原子元素进行访问。

You can access a one dimensional array through a pointer to its elements, and recursively applied, it follows that n-dimensional matrices can be accessed through pointers to the atomic elements.

Ç保证了他们连续内存的布局。这种模式是常见的。事实上传球阵列是很难或不可能做不同,给出的信息C商店的种类很少(而不是,比方说,C#)。所以矩阵衰变到一个指向它的第一个元素(在你的情况,以第一个维数组),它可以安全地转换为的及其的第一个元素,双重的地址。他们都有着相同的数字地址。

C guarantees a contiguous memory layout for them. This pattern is common. In fact passing arrays is hard or impossible to do differently, given the little information C stores with types (as opposed to, say, C#). So the matrix decays to a pointer to its first element (in your case to the first one dimensional array), which can safely be cast to an address of its first element, a double. They all share the same numerical address.

与指针类型的典型铸造的关注与现代的优化编译器混淆问题(编译器可以假设你不通过不相关的类型除外的char *指针的访问内存),但在标准草案1570一个明确的豁免,我有,比肩。 6.5.7,我认为这是适用在这里:

Typical concerns with pointer type casting are aliasing issues with modern optimizing compilers (a compiler can assume that you don't access memory through pointers of unrelated types except char*), but there is an explicit exemption in the standard draft 1570 which I have, par. 6.5.7, which I think is applicable here:

一个Object [这将是一个双在你的矩阵,-ps]应只能由左值访问其存储的价值
  具有下列类型之一前pression:

An object [that would be a double in your matrix, -ps] shall have its stored value accessed only by an lvalue expression that has one of the following types:

- 与有效对象的类型兼容的类型[这将是一个复引用双指针,-ps]
  [...]

— a type compatible with the effective type of the object [that would be a dereferenced double pointer, -ps] [...]

- 聚集体[例如阵列,-ps]
  [...]键入包含间上述类型之一的
  成员(包括递归,一个子聚集的成员,或
  包含工会),这将是你的矩阵变量,包含,最终,双打,-ps]

— an aggregate [e.g. array, -ps] [...] type that includes one of the aforementioned types among its members (including, recursively, a member of a subaggregate or contained union) [that would be your matrix variable, containing, eventually, doubles, -ps]

[...]

这篇关于什么是错传递一个二维数组到相应的指针参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆