gpt4 book ai didi

structure - C 中的嵌套数组结构

转载 作者:行者123 更新时间:2023-12-02 03:41:07 26 4
gpt4 key购买 nike

下面是它给我和我的代码的错误:

workspace.c: 在函数“main”中:

workspace.c:38: 错误:下标值既不是数组也不是指针

workspace.c:39: 错误:下标值既不是数组也不是指针

任何输入将不胜感激!谢谢你的时间。

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <dirent.h>
#include <sys/time.h>
#include <fcntl.h>

#define n 4
struct process
{
struct segment{

struct page
{
int pid;
int p[4];
}pages;
}segments;
};

struct process processes[3];
struct page pages[n];
struct segment segments[n];

int main()
{
int a, b, c;
int k = 0;
for(a = 0; a < 3; a++)
{
for(b = 0; b < 4; b++)
{
for(c = 0; c < 4; c++)
{
k++;
processes[a].segments[b].pages[c].pid = k;
printf("Segment: %d, Page: %d, PID: %d\n",a,b,processes[a].segments[b].pages[c].pid);
}
}

}

return 0;
}

最佳答案

查看此代码:Tested with no error

#define n 4
struct process
{
struct segment{

struct page
{
int pid;
int p[4];
}pages[n];
}segments[n];
}processes[3];


int main()
{
int a, b, c;
int k = 0;
for(a = 0; a < 3; a++)
{
for(b = 0; b < 4; b++)
{
for(c = 0; c < 4; c++)
{
k++;
processes[a].segments[b].pages[c].pid = k;
printf("Segment: %d, Page: %d, PID: %d\n",a,b,processes[a].segments[b].pages[c].pid);
}
}

}

return 0;
}

关于structure - C 中的嵌套数组结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20007963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com