Reputation:
I am getting an error:
error: request for member ‘clsf_ptr’ in something not a structure or union
From this C Program:
#include<stdio.h>
#define M 3
struct class_stud
{
char name[16];
char subject[16];
int roll_num;
}
struct node
{
int n;
struct branch *p[M];
struct class_stud cls[M-1];
}*root=NULL;
main()
{
int clsf_cnt;
struct class_stud clsf, *clsf_ptr;
clsf_ptr = &clsf;
clsf_cnt = 0;
root = malloc(sizeof (struct node));
printf("enter source address \n");
scanf("%s",&root.clsf_ptr[clsf_cnt]->name);
printf("%s",root.clsf_ptr[clsf_cnt]->name);
printf("enter key\n");
scanf("%d",&root.clsf_ptr[clsf_cnt]->key);
printf("%d",root.clsf_ptr[clsf_cnt] -> key);
clsf_cnt++;
}
What does the error mean?
Upvotes: 1
Views: 224
Reputation: 123458
There are a number of problems in this code. Let's walk through it line by line:
#include<stdio.h>
#define M 3
struct class_stud
{
char name[16];
char subject[16];
int roll_num;
}
First problem; you need a closing ;
after the struct definition above. Also, it's interesting that you don't define a preprocessor macro for the lengths of the name
and subject
arrays, but that's a minor issue at this point.
struct node
{
int n;
struct branch *p[M];
struct class_stud cls[M-1];
}*root=NULL;
Where is the definition for struct branch
? What is p
supposed to represent in the structure definition? Why do you have one fewer elements in cls
than p
?
main()
As of C99, you cannot get away with an implicit type for main
; it must be explicitly typed to return int
. And, since you're not messing with any command-line arguments, it's best to explicitly set the parameter list to void
, like so:
int main(void)
{
int clsf_cnt;
struct class_stud clsf, *clsf_ptr;
clsf_ptr = &clsf;
What purpose does the above operation serve?
clsf_cnt = 0;
root = malloc(sizeof (struct node));
That's fine, but a preferred way of writing this would be
root = malloc(sizeof *root);
That way you're sure to allocate the right amount of memory based on root
's type.
printf("enter source address \n");
scanf("%s",&root.clsf_ptr[clsf_cnt]->name);
Ack. Multiple problems in this one line.
First of all, clsf_ptr
is not a member of struct node
. You likely meant to access cls
. Secondly, since root
is a pointer to struct, you need to use the ->
operator to access its members (or explicitly dereference root
and then use .
). Third, cls[clsf_cnt]
is not a pointer type, so you would use the .
operator to access name
. Finally, since name
is an array type, it will implicitly be converted to a pointer type before being passed to scanf
, so the &
isn't necessary.
In order to avoid buffer overflow, you should either put a maximum field width in the %s
conversion specifier, or use fgets()
instead of scanf()
.
Sticking with scanf()
, that line should be written as
scanf("%15s", root->cls[clsf_cnt].name);
I'm using 15 instead of 16 in the conversion specifier so that we leave one character for the 0 terminator.
Using fgets(), that line would be written
fgets(root->cls[clsf_cnt].name, sizeof root->cls[clsf_cnt].name, stdin);
printf("%s",root.clsf_ptr[clsf_cnt]->name);
Again, you're trying to access something that isn't a member of root
, and you have your access operators backwards:
printf ("%s", root->cls[clsf_cnt].name);
printf("enter key\n");
scanf("%d",&root.clsf_ptr[clsf_cnt]->key);
printf("%d",root.clsf_ptr[clsf_cnt] -> key);
struct class_stud
has no member named key
; perhaps you meant roll_num
?
scanf("%d", &root->cls[clsf_cnt].roll_num);
printf("%d", root->cls[clsf_cnt].roll_num);
clsf_cnt++;
}
It looks like you've posted something that isn't complete; make sure you're showing us the same code you're trying to build.
Upvotes: 3
Reputation:
The following code should work for you. I'm assuming key
should be a member in class_stud
(and by your formatting options in scanf, a signed integer):
#include<stdio.h>
#define M 3
typedef struct
{
char name[16];
char subject[16];
int roll_num;
int key;
} class_stud;
typedef struct
{
int n;
struct branch *p[M];
struct class_stud cls[M-1];
} node;
int main(void)
{
int clsf_cnt = 0;
node *root;
root = malloc(sizeof(node));
if (root == NULL) {
/* handle error */
}
printf("enter source address \n");
scanf("%s",&root->cls[clsf_cnt]->name);
printf("%s",root->cls[clsf_cnt]->name);
printf("enter key\n");
scanf("%d",&root->cls[clsf_cnt]->key);
printf("%d",root->cls[clsf_cnt]->key);
clsf_cnt++;
}
You had the following problems:
key
was omittedint
, if you don't want arguments, tell the compiler that you expect argc to be void.Stuff I didn't do:
Upvotes: 0
Reputation: 213408
Here is some general source code cleanup that may be helpful:
#include <stdio.h>
#define M 3
typedef struct
{
char name [16];
char subject [16];
int roll_num;
} Class_stud;
typedef struct
{
int n;
Branch* p[M];
Class_stud cls[M-1];
} Node;
int main()
{
Node* root;
int clsf_cnt;
Class_stud clsf;
Class_stud* clsf_ptr;
clsf_ptr = &clsf;
clsf_cnt = 0;
root = malloc (sizeof(Node));
if(root == NULL)
{
/* error handling here */
}
}
Upvotes: 1
Reputation:
Any instance of root.clsf_ptr
you have in your code should be replaced with root->clsf_ptr
, which indicates that root
is a pointer to a struct.
You also need to make sure the member clsf_ptr
is located in the node
struct.
Upvotes: 1
Reputation: 4500
I think malloc syntax is wrong. once check that. Memory is not allocated.Thats why it shows an error.
Upvotes: -3