0

I supposed to perform a project which allocate N bytes of memory of struct person and scanning f every person's name initial_money and some other variables the problem for me when i run the code is that it is terminating at some point of taking input process and i don't why also this problem faced me yesterday in code forces contest

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

struct person
{
    char name[15];
    int initial_money;
    int g;
    int final_money;
    int money;

};

int main()
{

    int NP,i,j;
    char target1[15];
    scanf("%d",&NP);
    struct person *p=malloc(NP*sizeof(struct person));
    for(i=0;i<NP;i++)
    {
        scanf("%s",(p+i)->name);
    }
    for(i=0;i<NP;i++)
    {

        scanf("%s",target1);

        for(j=0;j<NP;j++)
        {
            if((p+j)->name==target1)
            {
                scanf("%d%d",(p+j)->initial_money,(p+j)->g);
                (p+j)->final_money=(p+j)->initial_money%(p+j)->g;
            }
        }
    }

    for(i=0;i<NP;i++)
    {

        printf("%s %d %d %d",(p+i)->name,(p+i)->initial_money,(p+i)->g,(p+i)->final_money);
    }
    return 0;
}

1 Answers1

2

The scanf function need pointers for inputed values. The line:

scanf("%d%d",(p+j)->initial_money,(p+j)->g);

Should be:

scanf("%d %d",&(p+j)->initial_money,&(p+j)->g);

When comparing strings you usually can't compare pointers directly:

 if((p+j)->name==target1)

shoul be:

 if(strcmp((p+j)->name, target1) == 0)
Joël Hecht
  • 1,766
  • 1
  • 17
  • 18
  • (p+j) points somewhere ok, but when it is followed by `->`, the pointer is dereferenced and it is no more a pointer. – Joël Hecht Mar 01 '15 at 08:58