简体   繁体   中英

Segmentation Fault(core dumped): Simple program but not working

#include <stdio.h>

pac()
{
    int i,j,k,size;

    char ns[size];
    int nss[size];
    printf("ENTER THE NUMBER OF STUDENTS: ");
    scanf("%d",size);
    for(i=0;i<size;i++)
    {
        printf("ENTER THE NAME OF STUDENT: ");
        scanf("%c",ns[i]);
    }
    for(j=0;j<size;j++)
    {
        printf("ENTER THE MARKS OF THE STUDENT: ");
        scanf("%d",nss[j]);
    }


    for(k=0;k<size;k++)
    {
        printf("%c",ns[i]);
    }
}

main()

{

pac();

}

I know the error is too small but I am new to C so kindly let me know the bug. There is a segmentation fault in this code after taking the first input of number of students.

Change

scanf("%d",size);  
           ^You missed & operator here.  

to

scanf("%d", &size);  

Now since you are using VLAs, you need to place your declaration of your VLAs after getting the value of size .

printf("ENTER THE NUMBER OF STUDENTS: ");
scanf("%d", &size);
char ns[size];
int nss[size];

size is not initialized and then you use it to set an array size, this is undefined behavior . As Fred said setting size later on won't re-size your arrays magically .

You should be calling scanf before you intialize the arrays but you need to pass in a pointer:

scanf("%d",&size);
           ^

You are also relying on implicit int for both main and pac , you should explicitly set their return type which should be int for main and probably void for pac .

You should be seeing warnings for most of these without even having to turn them on. Both gcc and clang provide warnings for implicit int returns and incorrect use of scanf , similar to this:

warning: return type defaults to 'int' [enabled by default]

warning: format '%d' expects argument of type 'int *', but argument 2 has type 'int' [-Wformat]

and turning up warnings also gets me a warning for using size initialized:

warning: 'size' is used uninitialized in this function [-Wuninitialized]

smells like a homework programming assignment...

#include <stdio.h>
pac()
{
    int i,j,k,size;

    printf("ENTER THE NUMBER OF STUDENTS: ");
    scanf("%d",&size); // <-- need to pass in the address of size

    do { // <-- you need this to establish the size for array creation
        char ns[size];
        int nss[size];

        for(i=0;i<size;i++)
        {
            printf("ENTER THE NAME OF STUDENT: ");
            scanf("%c",&(ns[i])); // <-- need the address here, too
        }
        for(j=0;j<size;j++)
        {
            printf("ENTER THE MARKS OF THE STUDENT: ");
            scanf("%d",&(nss[j])); // <-- and again here
        }


        for(k=0;k<size;k++)
        {
            printf("%c",ns[i]);
        }
    } while(0);
}

main()

{

    pac();

}

It's a combination of all answers.

  1. size is not initialized (already said)

  2. scanf's second argument must be a pointer (&size, &ns[i], ...)

  3. Allocate space dynamically based on value of size or have ns and nss with fix enough size (#define MAX_SIZE 1000 and check size < MAX_SIZE).

You can code it like this:

void pac()
{
    int i;
    int j;
    int k;
    int size;

    char *ns;
    int  *nss;

    printf("ENTER THE NUMBER OF STUDENTS: ");
    scanf("%d",&size);

    /* Check size */

    if (size > 0)
    {
        ns  = (char *)malloc(size * sizeof(char));
        nss = (int *)malloc(size * sizeof(int));

        /* check ns and nss are not NULL and then continue */

        for(i = 0; i < size; i++)
        {
            printf("ENTER THE NAME OF STUDENT: ");
            scanf("%c",&ns[i]);
        }

        for(j = 0; j < size; j++)
        {
            printf("ENTER THE MARKS OF THE STUDENT: ");
            scanf("%d", &nss[j]);
        }


        for(k = 0;k < size; k++)
        {
            printf("%c", ns[i]);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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