【问题标题】:Structure pointer dereference结构指针解引用
【发布时间】:2020-05-22 14:47:36
【问题描述】:

我正在尝试传递如图所示的点数组结构,如何正确取消引用地址以更改地址指向的值。

// header file "header.h"
typedef struct {
    double x;
    double y;
} Pointbase;
typedef Pointbase *XYpt;

typedef struct {
    XYpt xy[1];
} ChartPointsbase;
typedef ChartPointsbase **PointArray;

#include "header.h"
...

void npCluster(double drop, XYpt *newpt, PointArray outpts)
{
    double xx[2]={-15, 100};
    int i;
    outpts = (PointArray)malloc(sizeof(PointArray) * 2);
    for (i=0;i<2; i++) 
    {
        (*(*outpts)->xy[i])->x=xx[i];
        (*(*outpts)->xy[i])->y=drop;
    }
}

编译器喜欢以下行但不计算

        (*outpts)->xy[i]->y=drop;

任何建议将不胜感激。

【问题讨论】:

  • 我忘了补充,我想用这个函数来操作各种点数组来绘制图表。点数组的典型大小为 3000 个双点。
  • 您有一个包含一个指针的数组:XYpt xy[1]; 从 0 到 2 的索引将无效:-&gt;xy[i] [并且:请删除愚蠢的 typedef。他们只是令人困惑]

标签: pointers data-structures dereference


【解决方案1】:

我为“c”编译器计算如下: 使用两个大小为 dimsize 的 1D 数组定义 struct,为句柄分配内存,设置大小 =k 并取消引用,如下所示:

for (i=0; i<k; i=i++)
        {
            (*(outpts->xx))->dat[i]=135*i+j;
            (*(outpts->yy))->dat[i]=drop;
        }

为了进一步的嵌套,说上面的 struct array 有两个不相等的点数组,其中 cht 是 PointArray 的数组

typedef struct {
    int32 dimSize;
    C1Hdl cht[1];
} XYchartCluster;

// initialize 1st array
for (i=0; i<k; i=i++)
        {
            (*(*(xycht)->cht[0])->xx)->dat[i]=135*i+j;
            (*(*(xycht)->cht[0])->yy)->dat[i]=drop;
        }
// initialize 2nd array with values from point npt
for (i=0; i<sz; i=i++)
        {
            (*(*(xycht)->cht[1])->xx)->dat[i]=npt->x;
            (*(*(xycht)->cht[1])->yy)->dat[i]=npt->y;
        }


/*
Note: size of each array in chart should be initialized and 
memory assigned (dynamically changing size)
*/

【讨论】:

    猜你喜欢
    • 2015-07-13
    • 2011-04-28
    • 1970-01-01
    • 2020-08-19
    • 2013-01-04
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多