Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #18113

Re: passing a pointer to initialized structrue in a function

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: passing a pointer to initialized structrue in a function
Date 2012-03-02 11:45 -0500
Organization A noiseless patient Spider
Message-ID <4F50F91C.2070301@verizon.net> (permalink)
References <30546693.434.1330702765903.JavaMail.geo-discussion-forums@pbfa4>

Show all headers | View raw


On 03/02/2012 10:39 AM, ArifulHossain tuhin wrote:
> I have this structure like following :
> 
> typedef struct{
> char static_data[10];
> int a;
> int b;
> } my_ds;
> 
> i have initialized it like following:
> 
> my_ds * ds = (my_ds *)malloc(sizeof(my_ds));

It's better to do the following, it's less error prone and less likely
to cover up certain kinds of errors:

	my_ds *ds = malloc(sizeof *ds);

> memmove(my_ds->static_data, buf, len);
> ds->a = c;
> ds->b = d;
> 
> and passed it in a function:
> 
> int my_fun(void ** data);
> my_fun((void *)&ds);

ds object of type my_ds, and &ds is a pointer to that object. my_fun is
expecting to receive a pointer to an object of type void*, which is
completely different type of object. The result of your cast to void* is
to convert what would otherwise have been a constraint violation into
undefined behavior. It does not actually fix the problem. Here's one
approach that should work (assuming that this is the only problem in
your code):

	void *temp = &ds;
	my_fun(&temp);

If you still have problems, please put these code fragments together
into a single complete program, as simple as possible, that demonstrates
your problem. Make sure that it actually does demonstrate your problem.
Then post the entire program - that will make it a lot easier to figure
out. As it stands, your message left out a lot of details that might (or
might not) have been relevant:

1. The types and values of c, d, buf, and len.
2. The definition (NOT just the declaration!) of my_fun().
3. What you did to confirm that you were getting unexpected results.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

passing a pointer to initialized structrue in a function ArifulHossain tuhin <etothepowerpi@gmail.com> - 2012-03-02 07:39 -0800
  Re: passing a pointer to initialized structrue in a function ArifulHossain tuhin <etothepowerpi@gmail.com> - 2012-03-02 07:44 -0800
    Re: passing a pointer to initialized structrue in a function James Kuyper <jameskuyper@verizon.net> - 2012-03-02 11:26 -0500
  Re: passing a pointer to initialized structrue in a function James Kuyper <jameskuyper@verizon.net> - 2012-03-02 11:45 -0500
    Re: passing a pointer to initialized structrue in a function James Kuyper <jameskuyper@verizon.net> - 2012-03-02 11:47 -0500
    Re: passing a pointer to initialized structrue in a function Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-03-02 17:02 +0000
      Re: passing a pointer to initialized structrue in a function James Kuyper <jameskuyper@verizon.net> - 2012-03-02 13:53 -0500
  Re: passing a pointer to initialized structrue in a function Ike Naar <ike@iceland.freeshell.org> - 2012-03-02 17:25 +0000
  Re: passing a pointer to initialized structrue in a function cri@tiac.net (Richard Harter) - 2012-03-02 19:40 +0000

csiph-web