K&R p 130

M

mdh

Hi All,
Just when I thought things were going to get easy!

Structs.

I **thought** I had copied the examples pretty closely, but am getting
a number of errors.


The code:

#include <stdio.h>


int main (int argc, const char * argv[]) {

struct point {
int x;
int y;
};


struct point makepoint( int, int); /* error. previous decl of
'makepoint' was here*/



struct point p1 = makepoint(8,9);

printf("%d, %d\n", p1.x, p1.y);
}




struct point makepoint(int x, int y) {
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}

The error I got in the past has meant that something has been defined
twice, but as far as I can tell, ( probably incorrectly) I have
declared a struct called point, then declared a function which accepts
two integer arguments and returns a 'point' structure. Clearly I am
missing something.

In addition, I am getting a number of errors in makepoint function
defintion, which may become obvious once my initial query is cleared.
If not, I will ask then.

Lastly, K&R say , on p 130, of the makepoint function, "notice that
there is no conflict between the argument name and the member with the
same name". I assume that this refers to the lines

temp.x = x;
temp.y = y;

Thanks as usual.
 
S

santosh

mdh said:
Hi All,
Just when I thought things were going to get easy!

Structs.

I **thought** I had copied the examples pretty closely, but am getting
a number of errors.


The code:

#include <stdio.h>


int main (int argc, const char * argv[]) {

struct point {
int x;
int y;
};


struct point makepoint( int, int); /* error. previous decl of
'makepoint' was here*/



struct point p1 = makepoint(8,9);

printf("%d, %d\n", p1.x, p1.y);
}




struct point makepoint(int x, int y) {
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}

The error I got in the past has meant that something has been defined
twice, but as far as I can tell, ( probably incorrectly) I have
declared a struct called point, then declared a function which accepts
two integer arguments and returns a 'point' structure. Clearly I am
missing something.

In addition, I am getting a number of errors in makepoint function
defintion, which may become obvious once my initial query is cleared.
If not, I will ask then.

Lastly, K&R say , on p 130, of the makepoint function, "notice that
there is no conflict between the argument name and the member with the
same name". I assume that this refers to the lines

temp.x = x;
temp.y = y;

Thanks as usual.

Here's your code corrected:

#include <stdio.h>

struct point {
int x;
int y;
};

struct point makepoint( int, int);


int main (void) {
struct point p1 = makepoint(8,9);
printf("%d, %d\n", p1.x, p1.y);
return 0;

}

struct point makepoint(int x, int y) {
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}
 
M

mdh

Here's your code corrected:

#include <stdio.h>

struct point {
int x;
int y;

};

struct point makepoint( int, int);

snip

}



So I once again ran into a scope issue? In this case block scope? vs
file scope which you implemented? Santosh, could you help me
understand why I can declare 'int foo(int);' within "main" and then
call it, but I cannot do the same with the struct?
 
³

³Â·åÑï

So I once again ran into a scope issue? In this case block scope? vs
file scope which you implemented? Santosh, could you help me
understand why I can declare 'int foo(int);' within "main" and then
call it, but I cannot do the same with the struct?

The return type of 'int foo(int)' is "int", which is a built-in type
of the language, that means you can use it in the global
scope(wherever in your program).
But "struct point" is a type you defined yourself, and you have
defined it in function main(), so you have to use it just within the
local-scope of main. In your code, you defeined a function return type
struct point out of the local-scope of main, it breaks the rule
mentioned above.:)
 
M

mdh

The struct is not local to main. It is used in another function.
Similarly if you called foo() from anywhere except main(), you'd have to
prototype it again.
This is why we normally place structures and prototypes at the top of the
file, usually wrapped up into a header.


Well, this is really disheartening, as I thought I had it figured
out.


If we have:


int main ( void){

int foo( void);

int i = foo;

printf("...etc ");

return 0;
}


int foo (void){
do stuff;
return stuff;
}


This seems to compile and work. I also thought declaring foo() before
main allowed it to be "seen" throughout the translation unit, but
within main, only to be seen, but still to be legally used if I called
it as above. How then, did this differ from declaring the struct
"point" and function returning "point" within main and calling it from
within main. I hope this makes sense.
Thanks in advance for clarifying this.
 
M

mdh

Initializing an int object
with a function name
does not seem to compile and work.


OK...here is my tested example.

#include <stdio.h>

int main (int argc, const char * argv[]) {

double foo( double);
double d = foo(8);
printf( "%f", d); /* 14.000000 */
return 0;
}


double foo ( double d){
return 6.0 + d;
}
 
B

Ben Bacarisse

mdh said:
Initializing an int object
with a function name
does not seem to compile and work.

OK...here is my tested example.

#include <stdio.h>

int main (int argc, const char * argv[]) {

double foo( double);
double d = foo(8);
printf( "%f", d); /* 14.000000 */
return 0;
}

double foo ( double d){
return 6.0 + d;
}

Yes, a correct use of a function prototype with block scope. In your
struct example, the struct also had block scope and so was not
available at file scope when the function was defined. Nothing in the
definition of foo requires anything from the block of main so the
definition matches the prototype as required.
 
M

mdh

#include <stdio.h>
int main (int argc, const char * argv[]) {
   double foo( double);
   double d = foo(8);
   
snip
double foo ( double d){
   return 6.0 + d;
}

......... In your struct example, the struct also had block scope and so was not
available at file scope when the function was defined.  ...... Nothing in the
definition of foo requires anything from the block of main........

Firstly, thank you for answering.
So Ben, what am I missing, or is this just C fatigue? I **thought**
(obviously incorrectly :)), ( but without understanding why) that I
had, within the block of main,

declared the struct point, then declared a function called makepoint,
then outside of main, defined the function makepoint. I know what you
are saying about block scope, but on the face of it, why is this so
different from my example of foo that you say is correct. And...I
apologize in advance for belaboring this point.
 
B

Ben Bacarisse

mdh said:
#include <stdio.h>
int main (int argc, const char * argv[]) {
   double foo( double);
   double d = foo(8);
   
snip
double foo ( double d){
   return 6.0 + d;
}

......... In your struct example, the struct also had block scope and so was not
available at file scope when the function was defined.  ...... Nothing in the
definition of foo requires anything from the block of main........

Firstly, thank you for answering.
So Ben, what am I missing, or is this just C fatigue? I **thought**
(obviously incorrectly :)), ( but without understanding why) that I
had, within the block of main,

declared the struct point, then declared a function called makepoint,
then outside of main, defined the function makepoint.

Yes, but the definition is invalid that way. Let me write it out
compressed for convenience:

int main(void)
{
struct point { int x, y; }; /* The struct tag point has a */
struct point makepoint(int, int); /* meaning all the way down */
struct point p = makepoint(1, 2); /* to here... */
}

struct point makepoint(int a, int b) /* ... but not here where it */
{ /* is also needed. */
/* stuff */
}

Outside main, "struct point" has no meaning, so the definition is
rejected. The first error I get (always the one to worry about) is
"return type is an incomplete type" which means "I don't know enough
about struct point to make a function return one".

The complaint about a conflicting definition come later after the
compiler has guessed what you mean in order to keep going. I bet gcc
assumes an int return type in cases like this where the type in
invalid.
I know what you
are saying about block scope, but on the face of it, why is this so
different from my example of foo that you say is correct.

Your function foo does not rely on anything with a limited scope. Its
return type and parameter type are all keywords that have the same
meaning everywhere. If any of these types involved identifiers with
function scope, it too would have gone wrong.
And...I
apologize in advance for belaboring this point.

No need to apologise. The way Usenet works, I'd simply not reply if I
was in any way fed up with topic.
 
M

mdh

.  Let me write it out
compressed for convenience:

int main(void)
{
    struct point { int x, y; };       /* The struct tag point has a */
    struct point makepoint(int, int); /* meaning all the way down */
    struct point p = makepoint(1, 2); /* to here... */

}

struct point makepoint(int a, int b) /* ... but not here where it */
{                                    /* is also needed. */
  /* stuff */

}

Outside main, "struct point" has no meaning, so the definition is
rejected.


OK...I think I am getting it.
Is this then correct.

A struct is a type ( just like int is a type) but one which I ( the
programmer...if I can call myself that :) ) are able to declare. So,
essentially, up to now, when I have declared *anything* within main,
it has always been of a type that was ?"inherent" to C, unlike my own
type "point" which I need when defining "makepoint". So,
theoretically , "re-declaring" it just before the definition would
work, except it would be "clunky" and in any case, that is in essence
what one is doing by moving the declarations outside of main.
 
B

Ben Bacarisse

mdh said:
OK...I think I am getting it.
Is this then correct.

A struct is a type ( just like int is a type) but one which I ( the
programmer...if I can call myself that :) ) are able to declare. So,
essentially, up to now, when I have declared *anything* within main,
it has always been of a type that was ?"inherent" to C, unlike my own
type "point" which I need when defining "makepoint".

Sounds right.
So,
theoretically , "re-declaring" it just before the definition would
work,

Ah, no. Re-declaring it won't work. The standard says this:

"Moreover, two structure, union, or enumerated types declared in
separate translation units are compatible if their tags and members
satisfy the following requirements: ..."

[you can stop reading here if you like]

"... If one is declared with a tag, the other shall be declared
with the same tag. If both are complete types, then the following
additional requirements apply: there shall be a one-to-one
correspondence between their members such that each pair of
corresponding members are declared with compatible types, and such
that if one member of a corresponding pair is declared with a name,
the other member is declared with the same name. For two
structures, corresponding members shall be declared in the same
order. For two structures or unions, corresponding bit-fields shall
have the same widths."

The key part is "declared in separate translation units". Two
structs, declared in the same translation unit, even in separate
scopes, can't ever be compatible -- no matter how similar they look.

Because you were concentrating on scope, I chose the scope
explanation. Your struct tag is indeed out of scope when you define
makepoint (and that explains one error) but because of this other
paragraph you can't fix the code by re-declaring the struct later.
The solution to one (more the struct declaration to file scope),
happens to solve the other so I left it unsaid.
 
M

mdh

Your function foo does not rely on anything with a limited scope.  Its
return type and parameter type are all keywords that have the same
meaning everywhere.  If any of these types involved identifiers with
function scope, it too would have gone wrong.

One last little point. The identifier "foo" as in the following.


int main(void){

sometype foo ( some argument );

use foo;


}


sometype foo ( some arguments){};
}

When I call "foo" in main, that identifier does "know" where to find
the definition of foo. The identifier "foo" though has block scope, if
I understand this correctly.
So, how then does the call to foo get handled correctly?

Thank you.
 
B

Ben Bacarisse

mdh said:
One last little point. The identifier "foo" as in the following.


int main(void){

sometype foo ( some argument );

use foo;


}


sometype foo ( some arguments){};
}

When I call "foo" in main, that identifier does "know" where to find
the definition of foo. The identifier "foo" though has block scope, if
I understand this correctly.

No, there are two identifiers with the same letters and thus two
scopes. They are "linked" by the fact the declaration of foo in
main is, by default, a declaration of a function with extern linkage.
So, how then does the call to foo get handled correctly?

I don't know how to answer questions like this, because I don't see
the problem! Maybe I have answered it with the above clarification.
 
M

mdh

The key part is "declared in separate translation units".  Two
structs, declared in the same translation unit, even in separate
scopes, can't ever be compatible -- no matter how similar they look.

OK..., this is unique only to structs, unions and enums. And if so,
is this due to the complicated nature of those objects, and perhaps
the way C stores them internally? Or...that's just the way it
is? :)

If so, thanks again for clarifying this issue for me.
 
M

mdh

No, there are two identifiers with the same letters and thus two
scopes.  They are "linked" by the fact the declaration of foo in
main is, by default, a declaration of a function with extern linkage.

Thanks Ben...
 
M

mdh

The return type of 'int foo(int)' is "int", which is a built-in type
of the language, that means you can use it in the global
scope(wherever in your program).
But "struct point" is a type you defined yourself, and you have
defined it in function main(), so you have to use it just within the
local-scope of main. In your code, you defeined a function return type
struct point out of the local-scope of main, it breaks the rule
mentioned above.:)

Thank you.
 
B

Barry Schwarz

One last little point. The identifier "foo" as in the following.


int main(void){

sometype foo ( some argument );

use foo;


}


sometype foo ( some arguments){};
}

When I call "foo" in main, that identifier does "know" where to find
the definition of foo. The identifier "foo" though has block scope, if
I understand this correctly.
So, how then does the call to foo get handled correctly?

The call gets handled correctly because the linker, not the compiler,
is responsible for resolving the addresses.

During compilation, the compiler sees a prototype for foo at block
scope. It creates an entry for foo in an internal table that says foo
is a function being called (plus additional information about the
return and argument types). When you call foo, it generates data
(code?) in your object module that tells the linker that it needs to
find foo and adjust the data so that when this code is reached foo
will receive control. At the end of the block, it deletes the entry
for foo from the internal table because the prototype went out of
scope.

The compiler then sees the definition of foo which is at file scope.
It creates an entry for foo in an internal table that says foo is a
function being defined with external linkage (plus additional
information about return and argument types). {Note: since the
prototype is out of scope, the compiler cannot check that the
prototype and definition are consistent.} It also generates data in
your object module telling the linker that foo is here. At the end of
the function, the compiler does not clear the entry because the
definition is at file scope. Any subsequent code would be able to
"see" foo.

During linking, the linker builds tables of functions defined and
functions needed (using the data the compiler put in the object
module). Since foo appears in both, it uses the defined function to
provide the required adjustment at the point where foo is called. Any
functions needed that are not already defined in the object module
(like printf) are found by searching libraries.
 
B

Ben Bacarisse

mdh said:
OK..., this is unique only to structs, unions and enums. And if so,
is this due to the complicated nature of those objects, and perhaps
the way C stores them internally? Or...that's just the way it
is? :)

You got me. I don't know. It has the feel of being one of those
things where the rule that got picked is the simplest one that does
what is needed.
 
M

mdh

You got me.  .....


Thanks Ben.

Well, you enlightened me, esp with the ref to the "foo" declaration in
main, defined elsewhere.

" They are "linked" by the fact the declaration of foo in
main is, by default, a declaration of a function with extern linkage."



"
 
N

Nick Keighley

The struct is not local to main. It is used in another function.
Similarly if you called foo() from anywhere except main(), you'd have to
prototype it again.
This is why we normally place structures and prototypes at the top of the
file, usually wrapped up into a header.

why "usually"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

K&R p.97 8
p 145 K&R 14
Struct Member Variable Problems 1
Pointer to function in K&R C 16
Struct Member Variables Problem 0
struct point not identified by gcc 39
K$R xrcise 1-13 (histogram) 4
Lexical Analysis on C++ 1

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top