Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #105689
| From | T <T@invalid.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Need explanation of C syntax for a Perl 6 guy |
| Date | 2017-03-09 12:40 -0800 |
| Organization | A noiseless patient Spider |
| Message-ID | <o9seec$jrf$2@dont-email.me> (permalink) |
| References | <o9ql0c$di9$1@dont-email.me> <eicocnFmliiU1@mid.uni-berlin.de> |
On 03/09/2017 02:16 AM, Jens Thoms Toerring wrote:
> T <T@invalid.invalid> wrote:
>> Hi C!
>
>> I am trying to write some inline C code in Perl 6. Would
>> explain to me what is going on in the following two commands:
>
>> References:
>> https://tronche.com/gui/x/xlib/utilities/XStoreBuffer.html
>> https://tronche.com/gui/x/xlib/utilities/XFetchBuffer.html
>> man XStoreBuffer
>
>> int XStoreBuffer(Display *display, char *bytes,
>> int nbytes, int buffer);
>
> This is a function that returns a value of type 'int',
> indicating success (if it's 0) or failure. 'display'
> is a pointer to a structure of type ;Display', which
> contains information about the connection to the X server
> For several reasons structures are often not passed to
> functions by value (i.e. passing all the information they
> contain) but as a pointer to the location in memory where
> the structure can be found.
>
> 'buffer' is a variable of type 'pointer to char', i,e.
> the address of the memory region where the data you
> want to have stored reside. And since that address
> doesn't tell the function how many bytes there are to
> be stored you also need the 'nbytes' varialbe (of type
> 'int') to tell the function how many bytes from that
> memory location you want to store.
>
> Finally, 'buffer' is another 'int' variable and lets
> you tell the X server in which (of up to 8 I think)
> buffers you want to store those data.
>
>> char *XFetchBuffer(Display *display,
>> int *nbytes_return, int buffer);
>
> This function returns a 'pointer to char', which either
> is the address where to get the fetched data from or
> which is a NULL pointer in case the function failed.
> 'display' is the same thing as in the othe function call.
> 'bytes_return' is a pointer to an int. This is used by the
> function to return the number of bytes of the data it
> returned by storing that number at this location. And
> 'buffer' is again the number of the buffer.
>
> Here's a very primitive example:
>
> Display *d; // must be initialized somewhere!
> char mybuf[100]; // must be initialized somewhere!
> char *buf;
> int len;
>
> XStoreBuffer(d, mybuf, sizeof mybuf, 0);
> buf = XFetchBuffer(d, &len, 0);
>
> With the first call you ask the X server to store all the
> data form 'mybuf' ('sizeof nybuf' is lust the total size
> of that array) in its buffer numbered 0.
>
> The second call should return a pointer to wherever the
> X server stored the data. The '&len' argument is the
> address of your variable 'len' and the function will
> put the length of the buffer the X server has stored
> at that address, so after that call it will be the value
> of 'len'. And the last argument is again the buffer number.
>
>> Questions:
>> 1) what does the * do to the variable?
>
> They don't do anything to the variables, they just
> tell the comppiler that they're pointers. If you have
>
> int x;
>
> this defines a variable named 'x' that is of type 'int'
> (and has enough memory to hold all the of an 'int'). If you
> have
>
> int *pi;
>
> then you get a pointer to a variable of type 'int', i.e.
> basically the address of it. You could combine those
> two lines as
>
> 1 int x = 3;
> 2 T *pi;
> 3 px = &i;
> 4 printf("%d\n", *pi);
>
> In line 3 you assign the address of 'x' (the '&' is the
> "address of" operator) to the pointer 'px'. And in line
> 4 you print out what's stored at that address - the '*'
> in this context could be called the 'content of' operator.
>
> If you define a function that acceots an 'int' as its
> argument it could be
>
> void foo(int x);
>
> A function that expects a pointer to 'int' would be
>
> void bar(int *pi)
>
> if you want to call it with the address of an existing
> variable of type 'int' you'ld do
>
> int x = 13;
> bar(&x);
>
>> 2) what does the * do to the subroutine name?
>
> It doesn't do anything with the subroutine name, it merely
> says that the function will return a pointer.
>
>> 3) what is "int nbytes, int buffer"? Is "buffer"
>> the address (pointer) to the buffer? and "nbytes"
>> the length of "buffer"?
>
> 'nbytes' is the number of bytes you want to store and
> 'buffer' is the index of an internal buffer the X server
> keeps.
>
>> 4) does "int" stand for "integer"?
>
> It stands for a type that allows to store integers in a
> range that depends on the type of machine the program is
> running on. The only guarantee is that the range runs
> from -32767 to 32767 (but could be quite a bit larger).
>
>> 5) does "Display" stand for a special variable or a
>> routine?
>
> No, it's a structure containing information about which
> X server the function should interact with and its capa-
> bilities. This could be a server on a different machine
> and your program could also be interacting with several X
> servers. The details shouldn't be of concern to you. But
> if you really want to know what that structure is look
> into '/usr/include/X11/Xlibint,h' and search for the
> declaration of 'struct _XDisplay' (Display is just an
> alias for it).
>
>> 6) why does "*display" have a * in front of it?
>
> Because the function wants a pointer to a 'Display'
> structure.
>
>> 7) what does "char" do to "char *XFetchBuffer"?
>
> The 'char *' bit just indicates that the function will
> return a pointer to char, the first char of a set of
> 'nbytes_return' chars.
>
> One word of warning: as long as you're still struggling
> with basic concepts of Cd, as pointers, buffers etc. you
> have a big chance of making mistakes that can result in
> all kinds of weird errors. And then mixing that with the
> very low-level stuff from Xlib is a perfect recipe to
> get completely confused. If you use pointers in C you must
> be very aware of what you're doing, one mistake and all
> kinds of strange things can happen (with a segmentatuon
> fault being the most begnin!). I'm not trying to discourage
> you but before you start messing around with Xlib (which is
> in part that arcane that few people use it directly but
> instead some wrapper library) you better have a rather
> firm grasp of how C works. And once you start messing
> around with Xlib you will need good documentation (not
> easy to find, though), not just some random pages from
> the internet. "XLIB Programming Manual" by Adrian Nye
> is probably your best bet. Perhaps you better check
> carefully if what you want to achieve (which I don't
> know) can't be done with the graphics package you're
> rather likely already using.
>
> Regards, Jens
>
Thank you!
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-08 20:20 -0800
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-08 20:57 -0800
Re: Need explanation of C syntax for a Perl 6 guy Barry Schwarz <schwarzb@dqel.com> - 2017-03-09 00:46 -0800
Re: Need explanation of C syntax for a Perl 6 guy gordonb.txjrp@burditt.org (Gordon Burditt) - 2017-03-09 03:29 -0600
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-09 12:39 -0800
Re: Need explanation of C syntax for a Perl 6 guy jt@toerring.de (Jens Thoms Toerring) - 2017-03-09 10:16 +0000
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-09 12:40 -0800
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-09 13:25 -0800
Re: Need explanation of C syntax for a Perl 6 guy jt@toerring.de (Jens Thoms Toerring) - 2017-03-10 21:02 +0000
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-10 17:49 -0800
Re: Need explanation of C syntax for a Perl 6 guy Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-03-09 10:22 +0000
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-09 12:40 -0800
Re: Need explanation of C syntax for a Perl 6 guy GOTHIER Nathan <nathan.gothier@gmail.com> - 2017-03-09 12:48 +0100
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-09 12:43 -0800
Re: Need explanation of C syntax for a Perl 6 guy James Kuyper <jameskuyper@verizon.net> - 2017-03-10 10:22 -0500
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-10 11:47 -0800
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-10 12:42 -0800
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-10 19:52 -0800
Re: Need explanation of C syntax for a Perl 6 guy raltbos@xs4all.nl (Richard Bos) - 2017-03-09 12:09 +0000
Re: Need explanation of C syntax for a Perl 6 guy Keith Thompson <kst-u@mib.org> - 2017-03-09 09:05 -0800
Re: Need explanation of C syntax for a Perl 6 guy John Bode <jfbode1029@gmail.com> - 2017-03-09 12:31 -0800
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-09 12:41 -0800
Re: Need explanation of C syntax for a Perl 6 guy Paul <nospam@needed.invalid> - 2017-03-10 04:06 -0500
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-10 12:42 -0800
Re: Need explanation of C syntax for a Perl 6 guy jt@toerring.de (Jens Thoms Toerring) - 2017-03-10 21:35 +0000
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-10 17:48 -0800
Re: Need explanation of C syntax for a Perl 6 guy John Bode <jfbode1029@gmail.com> - 2017-03-10 14:54 -0800
Re: Need explanation of C syntax for a Perl 6 guy T <T@invalid.invalid> - 2017-03-10 17:43 -0800
Re: Need explanation of C syntax for a Perl 6 guy James Kuyper <jameskuyper@verizon.net> - 2017-03-10 21:11 -0500
csiph-web