Is this a function or a structure???(once again, a mind-boggling example from Kernighan)

Mark Jayson Alvarez jay2xra at yahoo.com
Tue Mar 8 02:26:48 PST 2005


Hi,

  On chapter 6 section 2 of The C Programming
Language, the topic is Structures and Function.

His very first example for this topic contains the
lines of code:

struct point makepoint(int x, int y)
{
  struct point temp;
  temp.x = x;
  temp.y = y;
  return temp;
}

As I can understand it, he is trying to declare a
structure of type "point", and the value of that
structure will be the return value of the function
"makepoint", or is it a function definition?

Can anyone enlighten me if this is really a structure
declaration or a function? If this is a structure,
then there must be a definition outside the main for
the function makepoint, right?Only that he didn't
showed anything.

But if this is already a function definition, like all
of his examples before, perhaps a simple program
should look like this

#include<...>
/* declare function makepoint */
/*i don't know how, do you?:) */

main()
{
/* call function make point and pass some arguments */
/* i'm not sure how to do this either.. help =( */

print("%d%d", newcreatedstruct.x,newlycreatedstruct.y)


}
struct point makepoint(int x, int y)
{
  struct point temp;
  temp.x = x;
  temp.y = y;
  return temp;
}

My problem is how am i going to call the function
makepoint. Why is there the word point in the above
definition of makepoint. 

Another thing is the words temp.x and temp.y, if they
are both members of structure temp, why didn't the
author declared it first, instead he immediately
initializing it to (x, y)

I'm reading the "Notes To accompany The C Programming
Language->
http://www.eskimo.com/~scs/cclass/krnotes/top.html"
but it didn't help me that much in this topic. Perhaps
you could. I have only 2 chapters left to read and I
can't wait to reach the topics unix system calls, and
sockets. But I don't want to leave this chapter
floating in the air as if I've never read it.

I really need all the help I can get.
Thank you very much.

Sincerely,
jayson











 




	
		
__________________________________ 
Celebrate Yahoo!'s 10th Birthday! 
Yahoo! Netrospective: 100 Moments of the Web 
http://birthday.yahoo.com/netrospective/


More information about the freebsd-questions mailing list