Reputation: 117
My query is relatively straightforward. There exists a structure called war_name
with the following definition:
struct war_name
{
char name[40];
char location[20];
int deaths;
int injuries;
char victor_name[20];
char loser_name[20];
};
The input function for the aforementioned structure I have created is as follows:
void enter_details(struct war_name *d)
{
printf("\n________________________________________\n");
printf("Enter the name of the battle: ");
fgets(d->name,sizeof(d->name),stdin);
printf("Enter the location: ");
fgets(d->location,sizeof(d->location),stdin);
printf("Enter the number of deaths: ");
scanf("%d",&d->deaths);
printf("Enter the number of injuries: ");
scanf("%d",&d->injuries);
printf("Enter the victor's name: ");
fgets(d->victor_name,sizeof(d->victor_name),stdin);
printf("Enter the defeated party's name: ");
fgets(d->loser_name,sizeof(d->loser_name),stdin);
printf("\n________________________________________\n");
}
Now, the main()
part has the following composition:
int main(void)
{
int n;
printf("How many war details do you wish to input?\n");
scanf("%d",&n);
struct war_name *ptr=(struct war_name *)malloc(n*sizeof(struct war_name));
if(ptr==NULL)
printf("\nInitialization unsuccessful\n");
for(int i=0;i<n;i++)
{
enter_details(ptr[i]);
}
exit(EXIT_SUCCESS);
}
Two queries arrive at this juncture.
Firstly, why does my compiler spit the error cannot convert 'war_name' to 'war_name*' for argument '1' to 'void enter_details(war_name*)'
when I pass the ptr
value directly to the enter_details()
function? To test the hypothesis that merely passing ptr[i]
to the function sends the location of the ith structure (which is what the function demands), I printed the value of ptr
directly, and lo and behold, the address of the said structure was printed. Why is it that despite this development, the compiler spits and error and demands that I pass the address of the structure with the &
operator, given that ptr[i]
itself holds the value of the ith structure variable?
Secondly, apropos the conception of the ptr
pointer (which points to the structure war_name
) and the usage of malloc()
, is the way I have initialized ptr
with the free space granted by the said function acceptable? For the sake of reference, be advised that the book I am following first creates a pointer of arrays to the war_name
struct and then passes the address of the said structures to the function? Is my method reasonable? Is it necessary to conceive an array of pointers to the said structures and then pass each structure variable's address to the enter_details()
function, given that my method saves valuable memory?
Upvotes: 0
Views: 148
Reputation: 2063
As said in the comments, there are issues in your code:
First:
enter_details(ptr[i]);
is wrong. enter_details()
accepts a pointer to struct war_name
while ptr[i]
is of type struct war_name
. So what you should do is pass the address of ptr[i]
:
enter_details(&ptr[i]);
Second, fgets()
reads the last \n
, so you should replace it with \0
:
fgets(d->name, sizeof(d->name), stdin);
d->name[strcspn(d->name, "\n")] = '\0';
Third, you should also avoid using scanf()
to read integers. Use fgets()
then parse the input with sscanf()
:
char buffer[255];
fgets(buffer, sizeof(buffer), stdin);
buffer[strcspn(buffer, "\n")] = '\0';
if (sscanf(buffer, "%d", &d->deaths) != 1)
printf("Could not read int\n");
Fourth,
struct war_name *ptr=(struct war_name *)malloc(n*sizeof(struct war_name));
is too long and too verbose. You don't need to cast the return value of malloc()
except if you use a C++ compiler:
struct war_name *ptr = malloc(n * sizeof(*ptr));
Upvotes: 3