0

This is the structure I have defined with the following members:

struct id{
    char name[32];
    int age;
    float iq;
};

This are print statements in one of the fucntions:

printf("Enter your age: ");
scanf("%d",p->age);

printf("Enter your iq: ");
scanf("%f",p->iq);

The issue is, the code compiles with 2 warning(s):

format '%d' expects argument of type 'int *', but argument 2 has type 
'int'.
format '%f' expects argument of type 'float *', but argument 2 has type 
'double'.

The output on Build/Run, returns:

segmentation failed(core dumped).

Any help would be much appreciated.

Also can someone mentor me, remotely? To become a genius at C-Programming ? :p

This is the entire source code of my program, cant understand why it wont work, the way I want it to. I found a workaround but I want to understand the concept behind the warning message(s).

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

/*declaring struct as global item*/
struct id{
    char name[32];
    int age;
    float iq;
};

/*prototyping*/
struct id *fAllocate(void);
void fFetch(struct id *p);
void fShow(struct id *p);

int main()
{
    struct id *author; //declaring the var of_type struct id

    /*allocate struct - fAllocate()_function*/
    author = fAllocate();

    /*fetch structure/populate - fFetch()_function*/
    fFetch(author);

    /*show struct - fShow()_function*/
    fShow(author);

    return 0;
}
/*fAllocate()_function*/
struct id *fAllocate(void){
    struct id *p;
    p = (struct id *)malloc(sizeof(struct id));
    if(p == NULL)
    {
        printf("Memory Unavailable!");
        exit(1);
    }
    else
    {
        return(p);
    }
    return;
}
/*fFetch()_fucntion*/
void fFetch(struct id *p){
    //declaring var to store scanf values
    /*
    char *n;
    int i;
    float f;
    */
    printf("Enter your name: ");
    scanf("%s",p->name //&n);
    //strcpy(p->name,n);

    printf("Enter your age: ");
    scanf("%d",p->age //&i);
    //p->age = i;

    printf("Enter your iq: ");
    scanf("%f",p->iq //&f);
    //p->iq = f;

    return;
}
/*fShow()_function*/
void fShow(struct id *p){
    printf("Author %s is %d years old!\n",
           p->name,
           p->age);
    printf("%s has an iq of %.2f!\n",
           p->name,
           p->iq);
    return;
}
utsav0195
  • 1
  • 4

1 Answers1

1

scanf() needs to be able to store the data it scans in. So it expects addresses, like this:

scanf("%d", &p->age);

and

scanf("%f", &p->iq);

POSIX documentation is here.

alk
  • 69,737
  • 10
  • 105
  • 255