I have these three files. using structures, functions and pointers.
Trying to do this:
void getName(Name *) - Receives a pointer to a Name and performs the actions
contacts.h
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
struct Contacts {
struct Name name;
struct Address address;
struct Numbers numbers;
};
void getName(struct Name*name);
contacts.c
#include <stdio.h>
#include "contacts.h"
void getName(struct Name*name)
{
char yorn;
printf("Please enter the contact's first name: ");
scanf("%s", name->firstName);
printf("Do you want to enter a middle initial(s)? (y or n): ");
scanf("%s", &yorn);
if (yorn == 'y'){
printf("Please enter the contact's middle initial(s): ");
scanf("%s", name->middleInitial);
}
printf("Please enter the contact's last name: ");
scanf("%s", name->lastName);
}
a1ms4.c
#include <stdio.h>
#include "contacts.h"
#include "contacts.c"
struct Name name;
getName(&contact.name);
I'm getting the following error in contacts.c:
error: dereferencing pointer to incomplete type struct Name
scanf("%s", name->firstName);
^~
[EDIT: I minimized the code to this:
#include <stdio.h>
struct Name {
char firstName[31];
};
struct Contacts {
struct Name name;
};
void getName(Name*name);
int main(void)
{
struct Contacts contact;
getName(&contact.name);
return 0;
}
void getName(Name*name)
{
char yorn;
printf("Please enter the contact's first name: ");
scanf("%c", name->firstName);
printf("First name: %c\n", name.firstName);
return;
}
and getting these error:
1). error: unknown type name âNameâ
void getName(Name*name);
2). In function âmainâ:
warning: implicit declaration of function âgetNameâ; did you mean âgetlineâ? [-Wimplicit-function-declaration]
getName(&contact.name);
3). At top level:
error: unknown type name âNameâ
void getName(Name*name)
*.cfiles! Ever. Until you are a pro and know exactly what you are doing.scanf("%s"with acharvariable address is a good idea? Read howscanfworks carefully...