[SOLVED] I do not understand 'makepoint' function and related code from K & R p130
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I do not understand 'makepoint' function and related code from K & R p130
The following code is from Kernighan and Ritchie page 130, the section on structures and functions. The makepoint function can "..initialize any structure dynamically, or to provide structure arguments to a function."
Code:
#include <stdio.h>
int main() {
struct rect screen;
struct point middle;
struct point makepoint(int, int);
screen.pt1 = makepoint(0, 0);
screen.pt2 = makepoint(10, 10);
middle = makepoint((screen.pt1.x + screen.pt2.x) /2,
(screen.pt1.y + screen.pt2.y) /2);
printf("%d, %d\n", screen.pt1);
}
/*makepoint: make a point from x and y components */
struct point makepoint(int x, int y) {
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}
Compiling the above code produces an embarrassment of warnings and errors, so as you can see I have no idea what I'm doing.
My question then is, how do I use K&R's code correctly? For example can I use makepoint() to print a midpoint in a rectangle?
I have read that the act of asking a question on here can clarify your thinking and it has helped me a bit. So I have reread the section in K&R and have got this far:
Code:
#include <stdio.h>
struct point
{
int x;
int y;
} pt1, pt2;
struct rect {
struct point pt1;
struct point pt2;
};
int main() {
struct rect screen;
struct point middle;
struct point makepoint(int, int);
screen.pt1 = makepoint(0, 0);
screen.pt2 = makepoint(10, 10);
middle = makepoint((screen.pt1.x + screen.pt2.x) /2,
(screen.pt1.y + screen.pt2.y) /2);
printf("%d, %d\n", screen.pt1);
printf("%d, %d\n", middle);
}
/*makepoint: make a point from x and y components */
struct point makepoint(int x, int y) {
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}
Which at least compiles, and I have printed the midpoint.
I am finding structures hard to understand, perhaps I need an easier starting place than K&R for them.
The following code is from Kernighan and Ritchie page 130, the section on structures and functions. The makepoint function can "..initialize any structure dynamically, or to provide structure arguments to a function."
Code:
#include <stdio.h>
int main() {
struct rect screen;
struct point middle;
struct point makepoint(int, int);
screen.pt1 = makepoint(0, 0);
screen.pt2 = makepoint(10, 10);
middle = makepoint((screen.pt1.x + screen.pt2.x) /2,
(screen.pt1.y + screen.pt2.y) /2);
printf("%d, %d\n", screen.pt1);
}
/*makepoint: make a point from x and y components */
struct point makepoint(int x, int y) {
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}
Compiling the above code produces an embarrassment of warnings and errors, so as you can see I have no idea what I'm doing.
My question then is, how do I use K&R's code correctly? For example can I use makepoint() to print a midpoint in a rectangle?
You have to provide defintions for struct point and struct rect in your code. You appear to be using them without ever defining them. (see pgs 128-129)
Providing those definitions will probably clear most of the embarrassment of errors!
Then your hilighted printf statement is both incorrect and incomplete.
It is incorrect in that the format specifiers are both for int types, %d, but you are providing a struct point as the argument for the first specifier. When you reference the member of a struct in this way you must provide the full specification of the member. Try screen.pt1.x, for example.
It is incomplete in that the format string specifies two arguments and you provide only one (and it is not the correct type as above).
Also, you declare main() as returning an int but it does not return anything.
As you have singled out this statament...
Quote:
The makepoint function can "..initialize any structure dynamically, or to provide structure arguments to a function."
Context is everything! You must understand that it is speaking specifically of the type of structure under discussion, i.e. struct point, and does not imply that it can initialize any type of structure whatever. And it does not avoid the need to provide the definitions, which I suspect was your point. (if not then pleaseelaborate)
printf("%d, %d\n", screen.pt1.x, screen.pt2.x);
printf("%d, %d\n", screen.pt1.y, screen.pt2.y);
printf("%d\n", middle); /* not sure how to do this part */
return 0;
}
/*makepoint: make a point from x and y components */
struct point makepoint(int x, int y) {
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}
Which returns
Code:
0, 10
0, 10
5
Which is not correct, so I am still misunderstanding. Also I still have %d for type int, as I'm not sure what I need for struct point format specifier.
You are completely correct in surmising that I failed to grasp the need to define struct rect and struct point in my first attempt.
I have read that the act of asking a question on here can clarify your thinking and it has helped me a bit.
I am finding structures hard to understand, perhaps I need an easier starting place than K&R for them.
Indeed! Trying to describe a problem to others is one of the best ways to improve your own understanding of it!
You posted while I was writing my first reply and on quick look you seem to have provided the required definitions. Now have a look at the printf specs for the next part.
K&R is about as basic and well written a source as you will find in my opinion. Don't overthink it!
You know what the basic types of the language are by now, int, char, float, etc. And you know that declaring a variable of some type causes it to exist (i.e. reserves space in memory for it).
Code:
int x;
int y;
char c;
... after which you can assign values to them and perform various operations on them, refering to them by name.
A struct defines a new type which is just a collection of other types which can be referenced collectively by a single name, the struct name, and individually by the struct name followed by the member operator '.' followed by the member name.
Code:
struct point {
int x;
int y;
}
This defines a new type named point which has two members of type int, x and y. It does not reserve space in memory for an instance of the type.
Code:
struct point middle;
This declares an instance of the type and reserves memory for it which you can reference by the name middle.
Code:
middle.x = 7;
middle.y = 3;
This assigns the values shown to each of the members of middle.
Code:
printf("%d,%d", middle.x, middle.y);
This uses the values stored in the members of middle to produce output.
Nothing confusing about that, I hope!
Good luck!
Last edited by astrogeek; 07-27-2022 at 01:57 PM.
Reason: tpoys
Which is not correct, so I am still misunderstanding. Also I still have %d for type int, as I'm not sure what I need for struct point format specifier.
There is no struct point specifier. Printf only has awareness of the basic types and specifiers for those. Printf cannot understand a struct point or any other structure, you have to provide specifiers for the member types and provide comlete member arguments when you reference them - structname.member - individually.
Also, reconsider your geometry lessons! Think about how you want to show those corner points! You are mixing coordinates from two different points. (i.e. should be 0,0 and 10,10 instead of 0,10 and 0,10 I think)
K&R is about as basic and well written a source as you will find in my opinion. Don't overthink it!
I agree, it is clear and well written. I have enjoyed using it to learn so far. I have not always found it basic, however if I reread something enough it always becomes clear in the end.
Thanks again astrogeek for your very patient responses. I would hazard a guess that teaching has played a part in your life? If not you have a natural talent for giving clear guidance that still gives the 'student' some thinking to do.
Also, reconsider your geometry lessons! Think about how you want to show those corner points! You are mixing coordinates from two different points. (i.e. should be 0,0 and 10,10 instead of 0,10 and 0,10 I think)
Yes, got that fixed. I'm blaming that on my old eyes, I still remember (x,y) from school. It was a long time ago though!
I got in a mess with the printf statements. Think they are untangled here:
I agree, it is clear and well written. I have enjoyed using it to learn so far. I have not always found it basic, however if I reread something enough it always becomes clear in the end.
Thanks again astrogeek for your very patient responses. I would hazard a guess that teaching has played a part in your life? If not you have a natural talent for giving clear guidance that still gives the 'student' some thinking to do.
You are very welcome!
Before you move on, now that you have the errors resolved, think again the importance of what you are being told here...
Quote:
Originally Posted by amikoyan
The makepoint function can "..initialize any structure (of the type) dynamically, or to provide structure arguments (of the type) to a function."
The makepoint function returns a value of type struct point.
That value can be used to initialize objects of the type, or can be assigned to objects of the type, or it can be used as an argument to another function which expects a value of the type.
Makepoint does not return an object of the type.
Just as this function...
Code:
int func(int arg){
int x;
x = arg * 7;
return x;
}
... returns a value of type int which may be used anywhere such a value can be used (i.e. initialization, assignment, as an argument to a function). But it does not return the object x or an int object that you can assign something else to!
K & R is a bit terse for learning from, although a classic of it's time.
I've got a copy lying around somewhere.
For a companion volume that's easier to learn from, try https://www.amazon.com/Book-Programm.../dp/0201183994
K & R is a bit terse for learning from, although a classic of it's time.
I've got a copy lying around somewhere.
For a companion volume that's easier to learn from, try https://www.amazon.com/Book-Programm.../dp/0201183994
Thanks for the link chrism01, sorry about the delay in replying, I have been away.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.