passing a pointer to initialized structrue in a function

  • Thread starter ArifulHossain tuhin
  • Start date
A

ArifulHossain tuhin

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));
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);

what happens inside my_fun, the first field, static[10] is initialized correctly but other two values are zero. What am i missing here?
 
A

ArifulHossain tuhin

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));
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);

what happens inside my_fun, the first field, static[10] is initialized correctly but other two values are zero. What am i missing here?

Sorry for dual posting. first one was not shown for a while. so i thought i did something wrong and posted again. group admin can delete one.
 
J

James Kuyper

On 03/02/2012 10:44 AM, ArifulHossain tuhin wrote:
....
Sorry for dual posting. first one was not shown for a while. so i thought i did something wrong and posted again. group admin can delete one.

There is no group administrator. Since you used google groups to post
it, you can view the message in google groups, and then select the
appropriate option (I don't remember what it was) to delete the message.
That will remove it from Google's archives, and may cause a request to
be sent out to other news servers asking them to remove their copies of
the message as well. However, such requests are routinely ignored by
most news servers, because they are so easily faked and have so
frequently been abused.

Basically, once you post to usenet, copies of your message are extremely
unlikely to ever completely disappear (unless you don't want them to -
Murphy rules).
 
B

Ben Bacarisse

James Kuyper said:
On 03/02/2012 10:39 AM, ArifulHossain tuhin wrote:

ds object of type my_ds, and &ds is a pointer to that object.

You've missed a level of indirection (or maybe just several words got
lost from the sentence). ds is an object of type pointer to my_ds. &ds
is indeed a pointer to "that object", but it's type is pointer to
pointer to my_ds.

<snip>
 
I

Ike Naar

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));
memmove(my_ds->static_data, buf, len);

Did you mean

memmove(ds->static_data, buf, len);

here ?
ds->a = c;
ds->b = d;

and passed it in a function:

int my_fun(void ** data);
my_fun((void *)&ds);

what happens inside my_fun, the first field, static[10] is initialized
correctly but other two values are zero. What am i missing here?

Apart from a few stylistic things, I don't see problems in the code that
could explain the behaviour (assuming c and d were nonzero, and that
len <= 10).
Can you show a *complete* small program (one that we can compile and run
without having to guess about the missing parts) that exhibits the faulty
behaviour?
 
J

James Kuyper

You've missed a level of indirection (or maybe just several words got
lost from the sentence).

I did lose a level of indirection (without, oddly enough, invalidating
my conclusion). I posted a correction to my code, but I should have
corrected the text, too:

"ds is pointer to an object of type my_ds, and &ds is a pointer to that
pointer."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top