Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > passing a pointer to initialized structrue in a function

Reply
Thread Tools

passing a pointer to initialized structrue in a function

 
 
ArifulHossain tuhin
Guest
Posts: n/a
 
      03-02-2012
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?
 
Reply With Quote
 
 
 
 
ArifulHossain tuhin
Guest
Posts: n/a
 
      03-02-2012
On Friday, March 2, 2012 9:39:25 PM UTC+6, 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));
> 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.
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      03-02-2012
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).

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      03-02-2012
On 03/02/2012 11:45 AM, James Kuyper wrote:
....
> void *temp = &ds;


Correction:
void *temp = ds;

> my_fun(&temp);

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-02-2012
James Kuyper <(E-Mail Removed)> writes:

> On 03/02/2012 10:39 AM, ArifulHossain tuhin wrote:

<snip>
>> my_ds * ds = (my_ds *)malloc(sizeof(my_ds));

<snip>
>> 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.


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>
--
Ben.
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      03-02-2012
On 2012-03-02, ArifulHossain tuhin <(E-Mail Removed)> 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));
> 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?
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      03-02-2012
On 03/02/2012 12:02 PM, Ben Bacarisse wrote:
> James Kuyper <(E-Mail Removed)> writes:
>
>> On 03/02/2012 10:39 AM, ArifulHossain tuhin wrote:

> <snip>
>>> my_ds * ds = (my_ds *)malloc(sizeof(my_ds));

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


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."



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
what data structrue book do you use or recommend? luciferleo C Programming 2 12-18-2005 10:02 AM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
passing pointer->struct->pointer->struct to function. .. ?? beetle C Programming 2 01-25-2005 06:08 PM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments