-1

I was trying to learn how structures are passed between functions and I wrote a program where there is an array of structure and structure itself has an array of integers.It is compiling properly but when I run it,the program is not expecting more than 4 values.I dont know what the error is?

#include<stdio.h>
#include<stdlib.h> 

typedef struct
{
    char bname[10];
    int ssn[3];
} book;

void accept(book k[], int n);
void print(book k[], int n);
int main()
{
    book a[2];
    accept(a, 2);
    print(a, 2);
    return 0;
}

void accept(book k[], int n)
{
    int i, j;

    for (i = 0; i < n; i++)
    {
        for (j = 0; j < 3; i++)
        {
            scanf("%d\n", &k[i].ssn[j]);
        }
        scanf("%s\n", k->bname);
    }
}

void print(book k[], int n)
{
    int i, j;
    for (i = 0; i < n; i++)

    {
        for (j = 0; j < 3; j++)
        {
            printf("%d\n", k[i].ssn[j]);
        }

        printf("%s\n", k->bname);
    }
}
LPs
  • 16,045
  • 8
  • 30
  • 61

1 Answers1

2

Your accept function:

for (j = 0; j < 3; i++)     // infinite loop

Should be j++ .

Next, in both accept and print change k->bname to k[i].bname, so as not to rewrite always the first object.

And as already pointed out in comments by @SouravGhosh why use scanf("%d\n"... - it can be just scanf("%d"....

Yuriy Ivaskevych
  • 956
  • 8
  • 18