linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Steve Graegert <graegerts@gmail.com>
To: _z33 <timid.Gentoo@gmail.com>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: typecasting - explain
Date: Fri, 9 Sep 2005 12:49:24 +0200	[thread overview]
Message-ID: <6a00c8d5050909034955f92d21@mail.gmail.com> (raw)
In-Reply-To: <dfrnos$tnv$1@sea.gmane.org>

On 9/9/05, _z33 <timid.Gentoo@gmail.com> wrote:
> Steve Graegert wrote:
> >
> >       char *s;
> >       int  *i;
> >
> >       s = (int *)malloc(128 * sizeof(char));
> >       i = (char *)malloc(128 * sizeof(int));
> >
> >       s = "malloc is cool!";
> >       printf("s: %s\n", s);
> >       printf("s: %d\n", s);
> >
> >       i = "malloc is cool!";
> >       printf("i: %s\n", i);
> >       printf("i: %d\n", i);
> >
> > This piece of code prints:
> >
> >       s: malloc is cool!
> >       s: 4350328
> >       i: malloc is cool!
> >       i: 4350328
> >
> 
>   The illustration was too simple and good, for me to understand.
>   Thanks :)
> 
> > You see, casting changes the interpretation of bits when they are
> > read.  The assigment of a particular type to a variable of another
> > type destroys the original type information (i.e. loss of precision).
> >
> 
>   destroys the original type information? Couldn't understand this part.
>   You are still able to recover the string, regardless of what kind of
> datatype you store in. The only disadvantage I that, this obscures the
> logic of the program.

Didn't mean this specific example, but when casting a float or double
to int then you loose some information which can not be restored
afterwards.

> > Yes function pointers are legal.  ANSI C99 says:
> >
> > "J.5.7 Function pointer casts
> > 1 A pointer to an object or to void may be cast to a pointer to a
> > function, allowing data to be invoked as a function (6.5.4).
> > 2 A pointer to a function may be cast to a pointer to an object or to
> > void, allowing a
> > function to be inspected or modified (for example, by a debugger) (6.5.4)."
> >
> > A function name is just a pointer to the memory location where the
> > function is found at runtime.  It can be queried, modified and cast to
> > other types.  It behaves like a variable.  Take a look at the
> > sigaction structure:
> >
> >       struct sigaction {
> >               /* SIG_DFL, SIG_IGN or pointer to function */
> >               void (*sa_handler)(int);
> >               ... /* some other fields */
> >       };
> >
> > You can define sigaction as follows:
> >
> >       void handler(int signo) {
> >               doneflag = 1;
> >       }
> >
> >       struct sigaction sa;
> >       sa.sa_handler = handler;
> >       ...
> >
> > Here, sa_handler is registered as a function to be called when the
> > specified signal occurrs.
> >
> > Just handle function pointers as simple pointer variables.
> 
>   Have still one silly question ---
>     since you say function pointers are similar to simple pointer
> variables, and that's why the typecast works, what would the following
> code mean?
> 
>   void handler1 (int a) {
> 
>     /* body of handler - 1 */
>     printf ("HANDLER - 1 \n");
> 
>   }
> 
>   void handler2 (int a) {
> 
>     /* body of handler - 2 */
>     printf ("HANDLER - 2 \n");
> 
>   }
> 
>   struct sigaction sa;
>   sa.sa_handler = (handler2 *) handler1;
> 
>   Is this possible? if so, what does it mean?

In this case you are trying to cast to a __name__ rather than a type. 
If you write

	sa.sa_handler = (void *)handler1;

it would be OK, but nothing would happen.  Why?  Because no
typecasting takes place (handler1 is of type void already) and
therefore nothing changes.  handler1 would be associated to
sa_handler.

A final example:

	#include <stdio.h>

	void func1(int);
	void func2(int);

	void func1(int i) {
		printf("func1: %d\n", i);
	}

	void func2(int i) {
		printf("func2: %d\n", i);
	}

	int main (void) {
		void (*f1)(int) = (void *)func1;
		/* the same as above */
		void (*f1)(int) = func1;
		void (*f2)(int) = func2;
		f1(1);
		f2(2);

		getc(stdin);

		return (0);
	}

Everything fine now?

Regards

	\Steve

--

Steve Graegert <graegerts@gmail.com>
Software Consultancy {C/C++ && Java && .NET}
Mobile: +49 (176)  21248869
Office: +49 (9131) 7126409

  reply	other threads:[~2005-09-09 10:49 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-09 19:46 typecasting - explain _z33
2005-09-09  7:33 ` kaushal
2005-09-09  8:48   ` _z33
2005-09-09  9:28     ` Jarmo
2005-09-09 10:04       ` _z33
2005-09-09 13:19         ` Glynn Clements
2005-09-09  9:46 ` Steve Graegert
2005-09-09 10:22   ` _z33
2005-09-09 10:49     ` Steve Graegert [this message]
2005-09-09 11:10       ` _z33
2005-09-09 11:29         ` Steve Graegert
2005-09-09 13:17 ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6a00c8d5050909034955f92d21@mail.gmail.com \
    --to=graegerts@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=timid.Gentoo@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).