By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,331 Members | 1,432 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,331 IT Pros & Developers. It's quick & easy.

Pointer passing

P: n/a
Hey All:

Any insight you could give me would be tremendously appreciated.

I have a data structure, one element of which takes the form:

struct.struct.ptr

Where ptr is a pointer to a struct. I initialize that ptr to NULL in
main(), and then pass the pointer as such to another function for
initialization:

init(struct.struct.ptr);

The init() prototype is as such:

init(mytype_t *ptr);

Clearly I'm doing something wrong: while ptr is correctly initialized
in init(), when I try to use it back in main() it's still NULL. How do
I correctly pass ptr such that it is properly made available after
initialization?

Thanks!

Regards,
John Quigley

Jun 17 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
qu******@gmail.com said:
init(struct.struct.ptr);

The init() prototype is as such:

init(mytype_t *ptr);

Clearly I'm doing something wrong: while ptr is correctly initialized
in init(), when I try to use it back in main() it's still NULL.
C is a pass-by-value language.
How do
I correctly pass ptr such that it is properly made available after
initialization?


Instead of passing its value, pass a value that represents its address.

void init(mytype_t *ptr) { *ptr = whatever; }

init(&this.that.ptr);

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 17 '06 #2

P: n/a
Richard,

Thank you kindly for your quick response.
init(&this.that.ptr);


Okay, I have tried doing this in the past. However, I get the
following warning at time of compilation:

warning: passing argument 1 of 'init' from incompatible pointer type.

I need to run, but will continue to look into this a bit later today.
Any thoughts as to why I would be getting this warning would help
tremendously. Thanks again.

Regards,
John Quigley

Jun 17 '06 #3

P: n/a
qu******@gmail.com said:
Richard,

Thank you kindly for your quick response.
init(&this.that.ptr);


Okay, I have tried doing this in the past. However, I get the
following warning at time of compilation:

warning: passing argument 1 of 'init' from incompatible pointer type.


That's because I said:

void init(mytype_t *ptr) { *ptr = whatever; }

but should have said:

void init(mytype_t **ptr) { *ptr = whatever; }

Note the extra *. Sorry about that.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jun 17 '06 #4

P: n/a
"qu******@gmail.com" <qu******@gmail.com> writes:
Richard,

Thank you kindly for your quick response.
init(&this.that.ptr);


Okay, I have tried doing this in the past. However, I get the
following warning at time of compilation:

warning: passing argument 1 of 'init' from incompatible pointer type.


You need to change both the call (so it passes a pointer-to-pointer)
and the function (so it expects a pointer-to-pointer). You also need
to change the body of the function so it operates on a
pointer-to-pointer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.