LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   I do not understand 'makepoint' function and related code from K & R p130 (https://www.linuxquestions.org/questions/programming-9/i-do-not-understand-makepoint-function-and-related-code-from-k-and-r-p130-4175715112/)

amikoyan 07-27-2022 12:18 PM

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?

amikoyan 07-27-2022 12:45 PM

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.

astrogeek 07-27-2022 01:23 PM

Quote:

Originally Posted by amikoyan (Post 6370289)
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)

sundialsvcs 07-27-2022 01:45 PM

"K&R" is a seminal book written by the authors themselves but it is not always the easiest one to read and understand.

amikoyan 07-27-2022 01:50 PM

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.

astrogeek 07-27-2022 01:52 PM

Quote:

Originally Posted by amikoyan (Post 6370296)
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!

astrogeek 07-27-2022 02:04 PM

Quote:

Originally Posted by amikoyan (Post 6370308)

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)

amikoyan 07-27-2022 02:14 PM

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.

amikoyan 07-27-2022 02:30 PM

Quote:

Originally Posted by astrogeek (Post 6370316)

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!

astrogeek 07-27-2022 03:51 PM

Quote:

Originally Posted by amikoyan (Post 6370317)
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 (Post 6370289)
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!

chrism01 08-05-2022 12:59 AM

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

amikoyan 08-09-2022 05:36 AM

Quote:

Originally Posted by chrism01 (Post 6371963)
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.


All times are GMT -5. The time now is 09:53 AM.