gpt4 book ai didi

c - 将结构添加到数组并更改其属性

转载 作者:行者123 更新时间:2023-11-30 21:05:37 24 4
gpt4 key购买 nike

    int numOfProcesses = 0;
printf("How many processes would you like to enter: ");
scanf("%d",&numOfProcesses);
struct process p[numOfProcesses];
int counter = 1;
// This is the quantum printf(argv[1]);
// This is the type to run printf(argv[2]);
if(argv[2] = "FCFS"){
while(counter < numOfProcesses){
int temp;
p[counter];
printf("For process %d: \n",counter);
printf("Enter the pid: ");
scanf("%d", p[counter].pid);
printf("Enter the burst time: ");
scanf("%d",p[counter].burstTime);
printf("Enter the arrival time: ");
scanf("%d",p[counter].arrivalTime);

counter++;
}

我试图让我的代码保存一个结构数组,并允许我编辑数组中结构的属性,但一切都会导致段错误。我在做什么?

最佳答案

我在代码中发现多个问题:

  1. if(argv[2] = "FCFS")

A.我认为目标是比较事物。它必须是“==”而不是“=”。但这同样不适用于字符串

B.对于字符串,您必须将其写为 if(!strcmp(argv[2], "FCFS")

  • p[计数器]行;不需要。它没有任何操作。

  • 计数器必须初始化为 0 而不是 1

  • 我不确定 struct process 的成员,但我假设它们都是整数。在 scanf 中,您必须传递成员的地址

  • scanf("%d", &p[counter].pid);

    scanf("%d", &p[counter].burstTime);

    scanf("%d", &p[counter].arrivalTime);

    如果它们是指向整数的指针,那么也会发生段错误,因为它们没有在代码中的任何地方初始化

    关于c - 将结构添加到数组并更改其属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53639406/

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