Reputation: 13381
when is do this i get
error: incompatible type for argument 1 of ‘display’
#define M 4
struct show
{
int value;
};
struct node
{
struct show keys[M-1];
};
void display(struct show *ptr)
main()
{
struct show key;
printf("Enter value:\n ");
scanf("%d",&keys.value);
display(keys);
}
void display(struct show *ptr)
{
printf("%d", ptr->value);
}
but when i give display(&key) there wont be any error, but when i pass keys as parameter to display it is like passing the address of the structure itself, why should i give &keys?
Upvotes: 0
Views: 1050
Reputation: 24587
keys
is a structure, but display
expects a pointer to a structure (you wrote *
for that purpose). Using display(&keys)
passes a pointer to the structure instead.
The point of passing a pointer instead of a structure is that C arguments are passed by value, which involves a copy. If you passed the structure, then a copy would be made which, given that a structure is usually pretty large, is an unnecessary waste of processing time. Passing a pointer involves only a copy of that pointer, which is faster.
Upvotes: 1
Reputation: 7982
It's tricky to tell what you're trying to do, but as far as compilation errors go, where you have written this:
scanf("%d",&keys.value);
display(keys);
I think you mean to write this:
scanf("%d",&key.value);
display(&key);
Upvotes: 2
Reputation: 5950
i think you meant this:
#include <stdio.h>
#define M 4
struct show
{
int value;
};
struct node
{
struct show keys[M-1];
};
void display(struct show *ptr);
main()
{
int i;
struct node node_instance;
for (i = 0; i < M-1; i++) {
printf("Enter value #%d:\n ", i);
scanf("%d",&node_instance.keys[i].value);
display(&node_instance.keys);
}
}
void display(struct show *ptr)
{
printf("%d\n", ptr->value);
}
Upvotes: 0
Reputation: 11797
You have to create a variable of struct node
type and pass its keys
address.
main()
{
struct node theNode;
// populate theNode's keys elements
display(theNode.keys);
}
Anyway, with the given code you will only print the first element of keys
collection, so you will need to update display()
function (i.e.: you will have to pass in the number of elements in keys
array).
Upvotes: 0
Reputation:
You're missing a semi-colon at the end of the prototype for display().
Also you need to indicate the return type from main, e.g. void main().
Upvotes: 0