0

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)

17
  • 5
    Don't include *.c files! Ever. Until you are a pro and know exactly what you are doing. Commented Dec 6, 2017 at 21:06
  • This is an assignment and they wanted us to include all three files Commented Dec 6, 2017 at 21:08
  • 3
    No way they are asking such a thing. If they are - I would reconsider studying C in this place. Commented Dec 6, 2017 at 21:09
  • 3
    @SaM Eugene is right, c files should never be included. c files should be compiled and linked (and in that sense, included into your output executable project) Commented Dec 6, 2017 at 21:10
  • 1
    Do you think using scanf("%s" with a char variable address is a good idea? Read how scanf works carefully... Commented Dec 6, 2017 at 21:22

3 Answers 3

1

When you say struct Foo {...}; in C, you are not defining a type with the name Foo. Instead, you are defining a struct by the name Foo **which is referenced as struct Foo.

Thus, when you say void getName(Name*name);, your compiler rightly complains that there is no type called Name. Since it could not parse this function declaration, it subsequently complains about not knowing about that function when you call it. Ignore that second error, it's not your problem. Fix the prototype.


There are two methods of fixing your program:

  1. Call your structs struct. At each and every instance where you have Name in your code, ensure that you are actually saying struct Name instead.

  2. Write a typedef that defines the type Name. Declare your struct like this:

    struct Foo { ... };
    typedef struct Foo Foo;
    

    Since C programmers are lazy gits, they will usually merge these two statements into the following declaration:

    typedef struct Foo { ... } Foo;
    

    This declares both the struct Foo and its alias Foo in a single statement. You'll see this a lot in real code.


The form of the typedef deserves some explanation. I find it easiest to think of it as a variable declaration with typedef prepended to turn the variable name into a type name. I.e. if you can declare an array of strings with

char* array[10];

then you can give a name to its type with

typedef char* ArrayType[10];

After that, you can declare the above array with a simple

ArrayType array;

So, when you say

typedef struct Foo Foo;

the second Foo is the name that is being declared, and the type it aliases is the rest of the declaration without the typedef: struct Foo.

Sign up to request clarification or add additional context in comments.

4 Comments

Yes I got it Thank You it fixed the error of unknown type. I put struct before the Name in the function. really appreciate the help
It is giving the error: request for member firstName in something not a structure or union
@SaM Ah, overlooked that mistake: When firstName is a pointer (declared with struct Name* firsName), you cannot use the . operator to access its members. You must use the dereferencing -> operator instead.
Now I am gonna try and put it separately in *.h *.c and main
0

There are quite a few things wrong but I think your main error is that you have declared Name as struct Name so you need to use struct Name instead of just Name.

2 Comments

Yes I put that Struct before the Name and its working now. Thank you for help. There is this new error: request for member firstName in something not a structure or union
I put every code in place (*.h *.c main) and now I am getting two kinds of errors in the header file one is Conflicting type with the get* second is redefination of struct *
0

Dereferencing a pointer means using the * operator to access the value stored at a pointer. But keep it in mind that the value stored at the address of the pointer must be a value of the same type as the type of variable the pointer "points" to, but there is no guarantee this is the case unless the pointer was set correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.