简体   繁体   English

c语言中的for循环

[英]for loop in c programming

#include<stdio.h>
int n;
struct process
{
    char name[2];
    int at;
    int bt;
    int priority;
    int st;
    int wt;
    int tat;
    int completed;
};

int main()
{
    int i;
    struct process p[n];
    
    printf("Enter no of processes:");
    scanf("%d",&n);
    
    i=0;
    while(i<n)
    {
        printf("\nenter name of process:");
        scanf("%s",p[i].name);
        
        printf("enter arrival time:");
        scanf("%d",&p[i].at);
        
        printf("enter burst time:");
        scanf("%d",&p[i].bt);
        
        printf("enter priority:");
        scanf("%d",&p[i].priority);
        
        p[i].completed=0;
        
        i++;
    }
    
}

In this code i have used a for loop for a program it has to work n times but it works for n-1 only.For example when n is 3 it is **printing 2 times only .could any one can explain reason?在这段代码中,我为一个程序使用了 for 循环,它必须工作 n 次,但它仅适用于 n-1。例如,当 n 为 3 时,它只打印 2 次。有人可以解释原因吗?

There is no for loop in your code.您的代码中没有 for 循环。

But anyway there are two distinct problems in your code:但无论如何,您的代码中有两个不同的问题:

int main()
{
    int i;
    struct process p[n];   // here the content of n is zero
                           // so you allocate space for zero processes
    
    printf("Enter no of processes:");
    scanf("%d",&n);        // here you ask the user to enter a value for n
                           // but it's too late, the memory for p has already
                           // been allocated based on the value of n which was 0

    

You want this:你要这个:

int main()
{
    int i;
    
    printf("Enter no of processes:");
    scanf("%d",&n);
    struct process p[n];
    ...

The second problem is here:第二个问题在这里:

struct process
{
    char name[2];   // name can only contain strin gs with a maximal length 
                    // of one (one char + the null char terminator
    ...

You probably want this (assuming your process names can be longer than 1 char):您可能想要这个(假设您的进程名称可以长于 1 个字符):

    char name[50]; room for 49 character + the null terminator

Don't do this:不要这样做:

int n;

int main() {
    struct process p[n];
    scanf("%d",&n);
}

When you do this, the value of n is uninitialized and defaults to 0. So struct process[n] declares an empty array of struct process .执行此操作时, n的值未初始化并默认为 0。因此struct process[n]声明了struct process的空数组。

Then when you do: scanf("%s",p[i].name) you are just writing to arbitrary memory on your stack, and will eventually hit a segfault.然后,当您执行以下操作时: scanf("%s",p[i].name)您只是在写入堆栈上的任意内存,最终会遇到段错误。

Instead do this:而是这样做:

int main() {
    int n, idx;
    struct process *p;
    scanf("%d", &n);
    p = (struct process *)malloc(sizeof(struct process) * n);

    for (idx = 0; idx < n; idx++) {
       printf("\nenter name of process:");
       scanf("%1s", p[idx].name);
       ...
    }
}

Using scanf is problematic too, but if you do make sure the string is no longer than the buffer that contains it plus space for the terminating '\\0' character.使用 scanf 也有问题,但如果您确实确保字符串不长于包含它的缓冲区加上终止字符'\\0'空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM