# Question about dealing with structures

#1

so im trying to wrap my head around the examples in the book concerning stucts, and have run into a few road blocks. 1) the first example

"// Here is the declaration of the struct Person
struct Person { float heightInMeters; int weightInKilos; };

int main(int argc, const char * argv[])
{ struct Person person…

why is it that we need to address person twice ({struct Person person)?

1. whats the difference between the two typedef declarations
(typedef struct
{ float heightInMeters; int weightInKilos; }
Person;

and

#include <stdio.h>
typedef struct _Person
{ float heightInMeters; int weightInKilos; }
Person;

and lastly 3) after we declare a typedef struct, how is it that we call the function "bodyMassIndex (Person p)? where is the little p from? how do we associate that with the code?

sorry for the 3 questions and thanks for any help.

#2

[quote]// Here is the declaration of the struct Person
struct Person { float heightInMeters; int weightInKilos; };

int main(int argc, const char * argv[])
{ struct Person person…

why is it that we need to address person twice ({struct Person person)?[/quote]

struct Person { float heightInMeters; int weightInKilos; }; is a declaration of an information element (a struct) for a person. It has no effect other than creating a type and thus telling the compiler that you intend to use it to create instances of it.

Where as struct Person person is creating an instance of that information element and giving it the name person. (This will result in the allocation of memory for it (on the stack in this case.)

For example:

[code]// Declare a Foo
struct Foo {char buf [26];};

void Bar ()
{
// Create two instances of Foo and populate them
struct Foo F1 = { ‘A’, ‘B’, ‘C’, …, ‘Z’};
struct Foo F2 = {‘a’, ‘b’, ‘c’, …, ‘z’;

// Do something with them

}[/code]

#3

[quote]2) whats the difference between the two typedef declarations
(typedef struct
{ float heightInMeters; int weightInKilos; }
Person;

and

#include <stdio.h>
typedef struct _Person
{ float heightInMeters; int weightInKilos; }
Person;[/quote]

They are the same, creating a type for an information element named Person consisting of a float and an int.
You can use this type to create an instance of Person as for example: Person child;

But if you try to use them in the same scope, you will get a compilation error.

For example:

[code]
// main.m

int main (int argc, const char * argv[])
{
// Declare a Foo
typedef struct {char buf [26];} Foo;
Foo foo1, foo2;

``````// Declare a second Foo
typedef struct Foo {char buf [52];} Foo;
Foo foo1, foo2;

return 0;
``````

}[/code]
This will cause the compiler to emit an error message that says something like typedef redefinition …

But this is okay:

``````//  main.m

int main (int argc, const char * argv[])
{
// Declare a Foo
typedef struct {char buf [26];} Foo;
Foo foo1, foo2;

{
// start an embedded scope

// Declare a second Foo
typedef struct Foo {char buf [52];} Foo;
Foo foo1, foo2;

// end of the embedded scope
}
return 0;
}``````

#4

awesome explanation. that helped immensely.