Reputation: 143
#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.
Upvotes: 2
Views: 130
Reputation: 5907
It's a combination of all answers.
size is not initialized (already said)
scanf's second argument must be a pointer (&size, &ns[i], ...)
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]);
}
}
}
Upvotes: 0
Reputation: 158529
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]
Upvotes: 0
Reputation: 142
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();
}
Upvotes: 0
Reputation: 106092
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];
Upvotes: 1