-2
#include <string.h>
#include<stdio.h>
#include<stdlib.h>

typedef struct bank
{
    char an;
    char name;
    char type;
    int bal;
};

int main()
{
    int i=0,n;

    printf("Enter the number of accounts\n");
    scanf("%d",&n);

    struct bank a[n];

    printf("Enter the details of the users\n");

    for(i=0;i<n;i++)
    {
        scanf("%s%s%s%d",a[i].an,a[i].name,a[i].type,&a[i].bal);
    }
    printf("The details of the users are\n");

    for(i=0;i<n;i++)
    {printf("%s\n%s\n%s\n%d\n\n",a[i].an,a[i].name,a[i].type,a[i].bal);}
    char atype[10];        

    printf("Enter the type of account you want to search\n");
    scanf("%s",atype);

    char typ[10];
    char s[]="savings";
    char c[]="current";

    int result,res1,res2;        
    result = strcmp(atype,s);
    if(result == 0)
    {
        for(i=0;i<n;i++)
        {
            typ[10] = a[i].type;
            res1 = strcmp(typ,s);
            if(res1 == 0)
            {
                printf("%s\n%s\n%s\n%d\n\n",
                       a[i].an,a[i].name,a[i].type,a[i].bal);
            }
            printf("\n");
        }
    } else
    {
        for(i=0;i<n;i++)
        {
            typ[10] = a[i].type;
            res2 = strcmp(typ,c);
            if(res2 == 0)
            {
                printf("%s\n%s\n%s\n%d\n\n", 
                       a[i].an,a[i].name,a[i].type,a[i].bal);
            }
            printf("\n");
        }
    }
}

so basically ik its my homework but i did everythimg and i still cannot resolve the segmentation fault.please help i think its something to do with strcmp() function but oh well i checked all the sources but couldnt really find any fix. any help would be appreciated.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • 2
    A `char` is not a `char *`. You need more than one `char` to hold a string. Also, `scanf()` needs to be passed *pointers*, so it knows where to write the data. Your code would probably segfault at the first `scanf()` when it tries to interpret an uninitialised `char` as a pointer, then store a string there. – Dmitri May 06 '18 at 06:41
  • Also, you don't need the `typedef` keyword just to define a struct... you haven't supplied a new type name anyway. Just `struct bank { ... };` will do instead of `typedef struct bank { ... };` – Dmitri May 06 '18 at 06:57
  • Where exactly does the segfault occur? If you do not like using a debugger (https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb) you can still find that out with generous printing of "I am here" messages. – Yunnosch May 06 '18 at 07:07
  • Especially print all return value of `scanf()`. Then compare each to the value as expected after reading the spec (http://en.cppreference.com/w/c/io/fscanf). Actually you should make your program double-check that there is no 0 returned anywhere. – Yunnosch May 06 '18 at 07:09
  • I recommend to reduce to smaller parts, get them working, then increase complexity. As described in this very helpful article: https://ericlippert.com/2014/03/21/find-a-simpler-problem/ – Yunnosch May 06 '18 at 07:13
  • You could start by reading each of those **13** compiler warnings that GCC emits with `-Wall -Wextra` – Antti Haapala -- Слава Україні May 06 '18 at 08:36

2 Answers2

0

For starters:

This

typ[10] = ...

accesses typ one past its valid memory. This invokes undefined behaviour, so anything can happen from then on.

In C array indexing is 0-based. So for char[10] the highest allowed index would be 9. Access the 1st element would be done by using 0.

alk
  • 69,737
  • 10
  • 105
  • 255
0

You have made 2 mistakes here .

First your struct bank declaration was wrong. You forgot to declare name an and type as string. You declared it as just character(char).It should be like :-

struct bank
{
    char an[100]; // assuming 100 is max size of input strings
    char name[100];
    char type[100];
    int bal;
};

second you cannot do typ[10] = a[i].type; you should use strcpy() Something like this :-

 strcpy(typ,a[i].type);

So this corrected code will work :-

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

struct bank       // change made 1
{
    char an[100];
    char name[100];
    char type[100];
    int bal;
};

int main()
{
    int i = 0, n;

    printf("Enter the number of accounts\n");
    scanf("%d", &n);

    struct bank a[n];

    printf("Enter the details of the users\n");

    for (i = 0; i < n; i++)
    {
        scanf("%s%s%s%d", a[i].an, a[i].name, a[i].type, &a[i].bal);
    }
    printf("The details of the users are\n");

    for (i = 0; i < n; i++)
    {
        printf("%s\n%s\n%s\n%d\n\n", a[i].an, a[i].name, a[i].type, a[i].bal);
    }
    char atype[10];

    printf("Enter the type of account you want to search\n");
    scanf("%s", atype);

    char typ[10];
    char s[] = "savings";
    char c[] = "current";

    int result, res1, res2;
    result = strcmp(atype, s);
    if (result == 0)
    {
        for (i = 0; i < n; i++)
        {
            strcpy(typ,a[i].type);            // change made 2
            res1 = strcmp(typ, s);
            if (res1 == 0)
            {
                printf("%s\n%s\n%s\n%d\n\n",
                       a[i].an, a[i].name, a[i].type, a[i].bal);
            }
            printf("\n");
        }
    }
    else
    {
        for (i = 0; i < n; i++)
        {
             strcpy(typ,a[i].type);               // change made 3
            res2 = strcmp(typ, c);
            if (res2 == 0)
            {
                printf("%s\n%s\n%s\n%d\n\n",
                       a[i].an, a[i].name, a[i].type, a[i].bal);
            }
            printf("\n");
        }
    }
}

So your mistake was not with strcmp()

anoopknr
  • 3,177
  • 2
  • 23
  • 33