LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-27-2022, 12:18 PM   #1
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 316

Rep: Reputation: 169Reputation: 169
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?
 
Old 07-27-2022, 12:45 PM   #2
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 316

Original Poster
Rep: Reputation: 169Reputation: 169
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.

Last edited by amikoyan; 07-27-2022 at 01:05 PM.
 
Old 07-27-2022, 01:23 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by amikoyan View Post
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)
 
Old 07-27-2022, 01:45 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
"K&R" is a seminal book written by the authors themselves but it is not always the easiest one to read and understand.
 
Old 07-27-2022, 01:50 PM   #5
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 316

Original Poster
Rep: Reputation: 169Reputation: 169
Thanks for the help astrogeek. Here is my attempt at putting your corrections in place:

Quote:
#include <stdio.h>
struct point makepoint(int, int);
struct point
{
int x;
int y;
} pt1, pt2;

struct rect {
struct point pt1;
struct point pt2;
};

int main() {

struct rect screen;
struct point middle;


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.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.
 
Old 07-27-2022, 01:52 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by amikoyan View Post
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
 
1 members found this post helpful.
Old 07-27-2022, 02:04 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by amikoyan View Post

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)

Last edited by astrogeek; 07-27-2022 at 02:08 PM.
 
Old 07-27-2022, 02:14 PM   #8
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 316

Original Poster
Rep: Reputation: 169Reputation: 169
I got in a mess with the printf statements. Think they are untangled here:

Quote:
printf("%d, %d\n", screen.pt1.x, screen.pt1.y);
printf("%d, %d\n", screen.pt2.x, screen.pt2.y);
printf("%d, %d\n", middle.x, middle.y);
Quote:
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.

Last edited by amikoyan; 07-27-2022 at 02:25 PM.
 
Old 07-27-2022, 02:30 PM   #9
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 316

Original Poster
Rep: Reputation: 169Reputation: 169
Quote:
Originally Posted by astrogeek View Post

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!
 
Old 07-27-2022, 03:51 PM   #10
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by amikoyan View Post
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 View Post
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!

Good luck!
 
Old 08-05-2022, 12:59 AM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
1 members found this post helpful.
Old 08-09-2022, 05:36 AM   #12
amikoyan
Member
 
Registered: Mar 2021
Distribution: Slackware64 -current
Posts: 316

Original Poster
Rep: Reputation: 169Reputation: 169
Quote:
Originally Posted by chrism01 View Post
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.

I will look into getting a copy of Kelley.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help me understand the usage of && and || in the below code new_user3085 Programming 11 10-05-2016 09:36 AM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
AOL UK && BT Voyager 100 && Slackware 10.2 && RP-PPPoE pitt0071 Linux - Networking 3 01-17-2006 06:10 AM
Ph&#7909;c h&#7891;i d&#7919; li&#7879;u b&#7883; m&#7845;t???, c&#7913; pollsite General 1 06-27-2005 12:39 PM
Gotta love those &#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&#1649;&# iLLuSionZ Linux - General 5 11-18-2003 07:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration