Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » CGoto page 1, 2, 3, 4  Next

K&R p 130

 
Jump to:  
 
mdh
PostPosted: Sun Aug 17, 2008 5:59 am    Post subject: K&R p 130
       
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:

Quote:


#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.
 

 
santosh
PostPosted: Sun Aug 17, 2008 5:59 am    Post subject: Re: K&R p 130
       
mdh wrote:

Quote:
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;
}
 

 
mdh
PostPosted: Sun Aug 17, 2008 6:25 am    Post subject: Re: K&R p 130
       
On Aug 16, 11:12 pm, santosh <santosh....@gmail.com> wrote:
Quote:


Here's your code corrected:

#include <stdio.h

struct point {
int x;
int y;

};

struct point makepoint( int, int);

snip


Quote:
}



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?
 

 
Malcolm McLean
PostPosted: Sun Aug 17, 2008 6:25 am    Post subject: Re: K&R p 130
       
"mdh" <mdeh@comcast.net> wrote in message
On Aug 16, 11:12 pm, santosh <santosh....@gmail.com> wrote:
Quote:

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 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.

--
Free games and programming goodies.
LINK
 

 
pete
PostPosted: Sun Aug 17, 2008 7:39 am    Post subject: Re: K&R p 130
       
mdh wrote:

Quote:
int foo( void);

int i = foo;

This seems to compile and work.

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

--
pete
 

 
³Â·åÑï
PostPosted: Sun Aug 17, 2008 8:30 am    Post subject: Re: K&R p 130
       
On 8ÔÂ17ÈÕ, ÏÂÎç2ʱ25·Ö, mdh <m...@comcast.net> wrote:
Quote:
On Aug 16, 11:12 pm, santosh <santosh....@gmail.com> wrote:





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?

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.Smile
 

 
mdh
PostPosted: Sun Aug 17, 2008 8:43 am    Post subject: Re: K&R p 130
       
On Aug 17, 1:25 am, "Malcolm McLean" <regniz...@btinternet.com> wrote:
Quote:
"mdh" <m...@comcast.net> wrote in message



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.
 

 
Ben Bacarisse
PostPosted: Sun Aug 17, 2008 11:50 am    Post subject: Re: K&R p 130
       
mdh <mdeh@comcast.net> writes:

Quote:
On Aug 17, 2:39 am, pete <pfil...@mindspring.com> wrote:

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(Cool;
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.

--
Ben.
 

 
Ben Bacarisse
PostPosted: Sun Aug 17, 2008 12:47 pm    Post subject: Re: K&R p 130
       
mdh <mdeh@comcast.net> writes:

Quote:
On Aug 17, 6:50 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
mdh <m...@comcast.net> writes:


#include <stdio.h

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

   double foo( double);
   double d = foo(Cool;
   

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 Smile), ( 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.

Quote:
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.

Quote:
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.

--
Ben.
 

 
Ben Bacarisse
PostPosted: Sun Aug 17, 2008 1:28 pm    Post subject: Re: K&R p 130
       
mdh <mdeh@comcast.net> writes:

Quote:
On Aug 17, 7:47 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
.  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 Smile ) 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.

Quote:
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.

--
Ben.
 

Page 1 of 4 .:. Goto page 1, 2, 3, 4  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates ©

rowery apteka długopisy Centrum Danych Gdańsk Tworzenie stron katowice