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

struct st *call(void);

int main()
{
struct st *p;
p=call();

printf("enter roll no.\n");
scanf("%d",&p->rollno);

printf("enter name\n");
scanf("%s",&p->name);

printf("enter marks\n");
scanf("%f",&p->marks);

printf("%d %s %f\n",p->rollno,p->name,p->marks);
}

struct st *call(void)
{
return malloc(sizeof(struct st));
}

Error:

str3.c: In function ‘main’: str3.c:15:14: error: dereferencing pointer to incomplete type ‘struct st’ scanf("%s",&p->name); ^ str3.c: In function ‘call’: str3.c:25:22: error: invalid application of ‘sizeof’ to incomplete type ‘struct st’ return malloc(sizeof(struct st));

1
that's explicit: scanf("%s",p->name);, don't pass address of the array/pointer, and for the other message, you have to provide the definition of st else sizeof cannot know its size. ` - Jean-François Fabre♦
Where is struct st defined? You need to include the definition. Are you missing an #include? - Johnny Mopp

1 Answers

0
votes

1) You had to add parentheses scanf("%d", &p->rollno) -> scanf("%d", &(p->rollno))

2) But you don't need parentheses at reading a string

Output:

enter roll no.
1
enter name
stackoverflow 
enter marks
3.14
1 stackoverflow 3.140000

Code:

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

struct st
{
    int     rollno;
    char    name[256];
    float   marks;
};

struct st   *call(void)
{
    return malloc(sizeof(struct st));
}

int         main()
{
    struct st   *p;

    p = call();

    printf("enter roll no.\n");
    scanf("%d", &(p->rollno));

    printf("enter name\n");
    scanf("%s", p->name);


    printf("enter marks\n");
    scanf("%f", &(p->marks));

    printf("%d %s %f\n", p->rollno, p->name, p->marks);
}