473,804 Members | 3,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

K&R p 130

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.

Aug 17 '08
34 1939
mdh
On Aug 17, 7:47*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
. *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.
Aug 17 '08 #11
mdh
On Aug 17, 7:47*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
>
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.

Aug 17 '08 #12
mdh <md**@comcast.n etwrites:
On Aug 17, 7:47Â*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
>>
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.
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.

--
Ben.
Aug 17 '08 #13
mdh
On Aug 17, 8:28*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
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.
Aug 17 '08 #14
mdh
On Aug 17, 8:47*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
>
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?
Thanks Ben...

Aug 17 '08 #15
mdh
On Aug 17, 1:30 am, ³Â·åÑï <BlueBlues.Che. ..@gmail.comwro te:
On 8ÔÂ17ÈÕ, ÏÂÎç2ʱ25·Ö, mdh <m...@comcast.n etwrote:
On Aug 16, 11:12 pm, santosh <santosh....@gm ail.comwrote:
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.:-)
Thank you.
Aug 17 '08 #16
On Sun, 17 Aug 2008 08:37:55 -0700 (PDT), mdh <md**@comcast.n et>
wrote:
>On Aug 17, 7:47*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
>>
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?
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.

--
Remove del for email
Aug 17 '08 #17
mdh <md**@comcast.n etwrites:
On Aug 17, 8:28Â*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
>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? :-)
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.

--
Ben.
Aug 17 '08 #18
mdh
On Aug 17, 11:06*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
>
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? *:-)

You got me. *.....
Ben.

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

"
Aug 17 '08 #19
On 17 Aug, 09:25, "Malcolm McLean" <regniz...@btin ternet.comwrote :
"mdh" <m...@comcast.n etwrote in message

On Aug 16, 11:12 pm, santosh <santosh....@gm ail.comwrote:
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.
why "usually"?

--
Nick Keighley
Aug 18 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
8581
by: Collin VanDyck | last post by:
I have a basic understanding of this, so forgive me if I am overly simplistic in my explanation of my problem.. I am trying to get a Java/Xalan transform to pass through a numeric character reference (i.e.  ) and it seems to be converting the character to its UNICODE representation. Take this source XML document: <?xml version="1.0" encoding="UTF-8"?>
1
11445
by: DrTebi | last post by:
Hello, I have the following problem: I used to "encode" my email address within links, in order to avoid (most) email spiders. So I had a link like this: <a href="mailto:DrTebi@yahoo.com">DrTebi</a> This would work like a regular mailto link in any browser, but wouldn't be visible to spiders if they don't have a function to decode it.
0
2425
by: Thomas Scheffler | last post by:
Hi, I runned in trouble using XALAN for XSL-Transformation. The following snipplet show what I mean: <a href="http://blah.com/?test=test&amp;test2=test2">Test1&amp;</a> <a href="http://blah.com/?test=test&amp;amp;test2=test2">Test2&amp;amp;</a> This results in the following HTML Code:
4
3036
by: Luklrc | last post by:
Hi, I'm having to create a querysting with javascript. My problem is that javscript turns the "&" characher into "&amp;" when it gets used as a querystring in the url EG: /mypage.asp?value1=1&amp;value2=4&amp; ... which of course means nothing to asp.
4
3230
by: johkar | last post by:
When the output method is set to xml, even though I have CDATA around my JavaScript, the operaters of && and < are converted to XML character entities which causes errors in my JavaScript. I know that I could externalize my JavaScript, but that will not be practical throughout this application. Is there any way to get around this issue? Xalan processor. Stripped down stylesheet below along with XHTML output. <?xml version='1.0'?>...
8
2819
by: Nathan Sokalski | last post by:
I add a JavaScript event handler to some of my Webcontrols using the Attributes.Add() method as follows: Dim jscode as String = "return (event.keyCode>=65&&event.keyCode<=90);" TextBox2.Attributes.Add("onKeyPress", jscode) You will notice that jscode contains the JavaScript Logical And operator (&&). However, ASP.NET renders this as &amp;&amp; in the code that is
11
6447
by: Jeremy | last post by:
How can one stop a browser from converting &amp; to & ? We have a textarea in our system wehre a user can type in some html code and have it saved to the database. When the data is retireved and
14
5942
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary changed to "&" by a browser when or other software, when editing and uploading the file to the web...
12
10124
by: InvalidLastName | last post by:
We have been used XslTransform. .NET 1.1, for transform XML document, Dataset with xsl to HTML. Some of these html contents contain javascript and links. For example: // javascript if (a &gt; b) ..... // xsl contents abc.aspx?p1=v1&amp;p2=<xsl:value-of select="$v2" />
7
4633
by: John Nagle | last post by:
I've been parsing existing HTML with BeautifulSoup, and occasionally hit content which has something like "Design & Advertising", that is, an "&" instead of an "&amp;". Is there some way I can get BeautifulSoup to clean those up? There are various parsing options related to "&" handling, but none of them seem to do quite the right thing. If I write the BeautifulSoup parse tree back out with "prettify", the loose "&" is still in there. So...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.