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

时间:2018-12-05 19:26:02

标签: c arrays struct

    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 个答案:

答案 0 :(得分:0)

我在代码中看到了多个问题:

  1. if(argv [2] =“ FCFS”)

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

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

  1. 第p [counter]行;不需要。它没有任何动作。

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

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

scanf(“%d”,&p [counter] .pid);

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

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

如果它们是整数的指针,则由于未在代码中的任何地方初始化它们,所以也会发生分段错误