【问题标题】:Invalid type of argument using pointer to struct array使用指向结构数组的指针的参数类型无效
【发布时间】:2015-11-14 23:59:15
【问题描述】:

我的下一个代码有问题,我收到错误:invalid type argument of unary '*' (have 'int')。如果我写了int *content 允许代码运行,但我必须写int=content 并更改代码*((ptab->content)+pC1+17),我尝试了但无法修复错误。

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>

using namespace std;

struct box{
    int content;
};
struct box *ptab;

int pC1=5;

int main (){
    ptab=new struct box[64];

    if (*((ptab->content)+pC1+17)==0) {
        pC1=pC1+17;
    }
    cout<<pC1<<endl;
}

我必须从指向指针的指针传递到结构数组,这段代码是一个例子,因为原始代码有 23000 行。

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <iomanip>
#include <stdio.h>
#include <Windows.h>

using namespace std;

int *box;

int pC1=5;

int main (){
    box=new int[64];

    if (*(box+pC1+17)==0){
                                        pC1=pC1+17;
                                    }
    cout<<pC1<<endl;
}

【问题讨论】:

  • 你不能取消引用这个表达式:((ptab-&gt;content)+pC1+17)
  • 你实际上想在这段代码中做什么?正确的输出是什么? (有多种方法可以编译它,但从你的问题中不清楚你想要哪一个 - 它们有不同的含义。)
  • 我很乐意提供比“重新考虑您在这里尝试的内容”更好的建议,但我不知道您要做什么。对此代码目标的更好解释可能会提供更好的结果。
  • 我还有其他游戏代码,该代码有 23000 行,我必须从指针传递到指向结构数组的指针
  • 我编辑问题并用指针编写代码,谢谢 =)

标签: c++ arrays pointers struct invalid-argument


【解决方案1】:

使用*((ptab-&gt;content)+pC1+17),更简单的说法是ptab[pC1+17].content [后者编译并生成 22 作为输出]。你是说那个还是ptab-&gt;content + pC1 + 17 [产生5]?

【讨论】:

  • 我必须从指针传递到结构数组的指针,这是我的错误,对不起
猜你喜欢
  • 1970-01-01
  • 2017-06-03
  • 2012-12-07
  • 2016-06-20
  • 1970-01-01
相关资源
最近更新 更多