Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #18110 > unrolled thread
| Started by | ArifulHossain tuhin <etothepowerpi@gmail.com> |
|---|---|
| First post | 2012-03-02 07:39 -0800 |
| Last post | 2012-03-02 19:40 +0000 |
| Articles | 9 — 5 participants |
Back to article view | Back to comp.lang.c
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
| From | ArifulHossain tuhin <etothepowerpi@gmail.com> |
|---|---|
| Date | 2012-03-02 07:39 -0800 |
| Subject | passing a pointer to initialized structrue in a function |
| Message-ID | <30546693.434.1330702765903.JavaMail.geo-discussion-forums@pbfa4> |
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?
[toc] | [next] | [standalone]
| From | ArifulHossain tuhin <etothepowerpi@gmail.com> |
|---|---|
| Date | 2012-03-02 07:44 -0800 |
| Message-ID | <31199210.506.1330703048396.JavaMail.geo-discussion-forums@pbls8> |
| In reply to | #18110 |
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.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-03-02 11:26 -0500 |
| Message-ID | <4F50F49B.8060907@verizon.net> |
| In reply to | #18111 |
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).
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-03-02 11:45 -0500 |
| Message-ID | <4F50F91C.2070301@verizon.net> |
| In reply to | #18110 |
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.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-03-02 11:47 -0500 |
| Message-ID | <4F50F987.5080602@verizon.net> |
| In reply to | #18113 |
On 03/02/2012 11:45 AM, James Kuyper wrote: ... > void *temp = &ds; Correction: void *temp = ds; > my_fun(&temp);
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2012-03-02 17:02 +0000 |
| Message-ID | <0.5ddcb5df8d3f9eb55b71.20120302170212GMT.87pqcvhr6z.fsf@bsb.me.uk> |
| In reply to | #18113 |
James Kuyper <jameskuyper@verizon.net> 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.
[toc] | [prev] | [next] | [standalone]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Date | 2012-03-02 13:53 -0500 |
| Message-ID | <4F511731.5010802@verizon.net> |
| In reply to | #18115 |
On 03/02/2012 12:02 PM, Ben Bacarisse wrote: > James Kuyper <jameskuyper@verizon.net> 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."
[toc] | [prev] | [next] | [standalone]
| From | Ike Naar <ike@iceland.freeshell.org> |
|---|---|
| Date | 2012-03-02 17:25 +0000 |
| Message-ID | <slrn3vfsjl20jd.dls.ike@iceland.freeshell.org> |
| In reply to | #18110 |
On 2012-03-02, ArifulHossain tuhin <etothepowerpi@gmail.com> 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?
[toc] | [prev] | [next] | [standalone]
| From | cri@tiac.net (Richard Harter) |
|---|---|
| Date | 2012-03-02 19:40 +0000 |
| Message-ID | <4f5121d1.1255680892@text.giganews.com> |
| In reply to | #18110 |
On Fri, 2 Mar 2012 07:39:25 -0800 (PST), ArifulHossain tuhin
<etothepowerpi@gmail.com> 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?
Possibly that c and d are zero. You need to show us the code that
gives them values.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web