c - 如何将位于结构数组内的一个指针内容复制到另一个指针

标签 c pointers struct

我有以下列表,其中一个是用连接结构制作的包,一个是卡车,是用静态数组制作的。

typedef struct
{
    int id_pack;
    int weight_kg;
    int value;
    int priority;           //low 0, high 1
    struct pack* next;
} pack;

typedef struct pack head;  //declaring a struct head of type pack which is the head of a list of packs

typedef struct
{
    int id_truck;
    head* p_head;            //pointer to the list of packs of each truck
} truck;

int main()
{
    truck dogana[N];        //list of trucks made with a static array
}

现在,我想创建一个函数来检查第一辆卡车的包裹列表中至少一个包裹的优先级是否为高优先级 (1)。 为了做到这一点,我想到了制作一个临时指针以避免丢失对包列表头部的引用(我需要它来做我必须在函数内部做的其他事情);这是我的职责。

void uscita_dogana (truck dogana[])
{
    head* temp;
    temp = dogana[0].p_head; // I use temp to avoid losing the head 

    while (temp != NULL)   //cicle to run trough the packs
    {
        if (temp->priority == 1) //check the priority of the pack
        {
           //INSTRUCTIONS
        }
    }
}

现在编译器给我一个错误 临时->优先级== 1 上面写着“取消引用指向不完整类型'head {aka struct pack}的指针;

我尝试了很多不同的方法来解决这个问题,但没有成功。 我寻求帮助并在网站上搜索但找不到合适的解决方案,我希望有人能帮助我!

对于大家的困惑,我深表歉意,英语不是我的第一语言,我才刚刚开始编码。提前致谢!

最佳答案

您已将类型 pack 定义为匿名结构的别名(请注意,您在 typedef struct { 中的关键字 struct 后没有标识符。 .. } 包.

当你稍后定义

typedef struct pack head;

那么你指的是尚未定义的struct pack。编译器会将此标记为不完整类型,这样 head 也将是不完整类型。这就是为什么 temp->prioritytemp 的类型 head 未知的原因。

typedef pack head;

它应该可以工作。

关于c - 如何将位于结构数组内的一个指针内容复制到另一个指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54295390/

相关文章:

c - C 中的表达式 "double (f)(double)"可能是什么意思

C、表达式必须是可修改的左值(改变结构成员的值)

c++ - 尝试将指针分配给结构时程序崩溃

c - 当指定的格式是字符时接受字符串

c - 二叉搜索树递归和 ma​​lloc

c++ - 将值传递给动态分配的数组时出现段错误

c - 文本文件中每个单词的频率。分配内存时出错?

C : array of struct (which holds an int and another array of another struct)

c - 结构有两个字段,每个字段都是一维数组。将结构对象传递给没有指针的函数,会改变结构对象吗?

c - 宏中的参数计数