简体   繁体   English

c++ 错误:指向数组的指针

[英]c++ error : pointer that points to an array

In XCode it says:Thread 1: EXC_BAD_ACCESS (code=1, address=0x3c000003e7)在 XCode 中它说:线程 1:EXC_BAD_ACCESS(代码=1,地址=0x3c000003e7)

#include <stdio.h>
#include "Assistance.h"
#define infity 999

using namespace std;
class WeightedDirGraph
{
public:
    WeightedDirGraph();
    WeightedDirGraph(int **g, int size);
    ~WeightedDirGraph();
    void BellmanFord();
    void Display();
protected:
    int size;
    int (*(*graph));

};

WeightedDirGraph::WeightedDirGraph(int **g, int s)
{
    int a[s][s];
    graph=(int **)new int *[s];
    for(int i=0;i<s;++i)
    {
        for(int j=0;j<s;++j)
        {
            printf("%d ",*(*(g+i)+j));      //THIS IS THE LINE OF CODE THAT SEEMS TO BE WRONG
        }
        cout<<endl;
    }
    size=s;  
}



int main()
{

  char vexs[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', };
  int m[7][7] =
    {
      {infity,     60,     50,     50,  infity, infity,  infity},
      {infity, infity, infity, infity,     -10, infity,  infity},
      {infity,    -20, infity, infity,      10, infity,      70},
      {infity, infity,    -20, infity,  infity,    -10,  infity},
      {infity, infity, infity, infity,  infity, infity,      30},
      {infity, infity, infity, infity,  infity, infity,      30},
      {infity, infity, infity, infity,  infity, infity,  infity}
    };
    WeightedDirGraph *g=new WeightedDirGraph((int **)m,7);


}

I am trying to pass a pointer that points to a two dimensional array and initialize this WeightedDirGraph.我试图传递一个指向二维数组的指针并初始化这个 WeightedDirGraph。 But I can't run this code.但我无法运行此代码。 I think there must be some sort of mistake when I am using the pointer, but I don't know what the problem is.我认为当我使用指针时一定有某种错误,但我不知道问题是什么。 Can someone help me?有人能帮我吗? Thanks a lot.非常感谢。

An array is not a pointer, and no amount of casting can change that fact.数组不是指针,再多的转换也无法改变这一事实。
*(g+i) (ie g[i] ) is m[i] , which is an int[7] , not an int* . *(g+i) (即g[i] )是m[i] ,它是int[7] ,而不是int*
When you interpret its first elements as an address, hilarity ensues.当您将其第一个元素解释为地址时,就会出现欢闹。

You need to start with an array of pointers, since that can be converted to a pointer to a pointer.您需要从指针数组开始,因为它可以转换为指向指针的指针。

With minimal changes to your code, it might look like this:只需对代码进行少量更改,它可能如下所示:

int m[7][7] =
{
  {infity,     60,     50,     50,  infity, infity,  infity},
  {infity, infity, infity, infity,     -10, infity,  infity},
  {infity,    -20, infity, infity,      10, infity,      70},
  {infity, infity,    -20, infity,  infity,    -10,  infity},
  {infity, infity, infity, infity,  infity, infity,      30},
  {infity, infity, infity, infity,  infity, infity,      30},
  {infity, infity, infity, infity,  infity, infity,  infity}
};
int* mp[7] = {m[0], m[1], m[2], m[3], m[4], m[5], m[6]};
WeightedDirGraph *g=new WeightedDirGraph(mp, 7);

The better solution is probably to rethink your implementation and store the data in a one-dimensional std::vector .更好的解决方案可能是重新考虑您的实现并将数据存储在一维std::vector中。
Also, g[i][j] is much easier to follow than *(*(g+i)+j) .此外, g[i][j]*(*(g+i)+j)更容易理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM