473,795 Members | 2,968 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 #1
34 1937
mdh wrote:
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;
}
Aug 17 '08 #2
mdh
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?
Aug 17 '08 #3

"mdh" <md**@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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Aug 17 '08 #4
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.:-)
Aug 17 '08 #5
mdh
On Aug 17, 1:25*am, "Malcolm McLean" <regniz...@btin ternet.comwrote :
"mdh" <m...@comcast.n etwrote 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.


Aug 17 '08 #6
mdh wrote:
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
Aug 17 '08 #7
mdh
On Aug 17, 2:39*am, pete <pfil...@mindsp ring.comwrote:
>

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

Aug 17 '08 #8
mdh <md**@comcast.n etwrites:
On Aug 17, 2:39Â*am, pete <pfil...@mindsp ring.comwrote:
>>
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.

--
Ben.
Aug 17 '08 #9
mdh
On Aug 17, 6:50*am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
mdh <m...@comcast.n etwrites:
#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 sowas 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.
Aug 17 '08 #10

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
3030
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
6445
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
5936
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
10121
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
4629
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
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
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
10215
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
10165
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
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
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
5437
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.