[SOLVED] Passing pointers through multiple functions.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to pass a pointer through multiple layers of functions and then write data to the original memory where the first pointer was allocated but it keeps crashing.
I think you're trying to be too complicated. You can send the address of a pointer.
Code:
unsigned char **pointer_to_a_pointer_to_an_unsigned_char;
unsigned char *pointer_to_an_unsigned_char;
some_function(&pointer_to_an_unsigned_char); // this sends the ADDRESS of the pointer_to_an_unsigned_char to the function and allows some_function to modify that pointer's contents.
// Same result
pointer_to_a_pointer_to_an_unsigned_char = &pointer_to_an_unsigned_char;
some_function(pointer_to_a_pointer_to_an_unsigned_char);
// Note: Prototype for this function can be either of these two, however the second one is the better way to do this to ensure people understand the intentions of the function:
void some_function(unsigned char *);
void some_function(unsigned char **);
// See also information about (int argc, char **argv), same sort of conventions
My particular application is pretty specific to Linux. It possible I could add support BSD by using divert but so far there have been zero requests for that. Thanks for the info seems like a good thing to know.
Your code would be much clearer if you consistently refer to e.g. struct record * instead of using void * or __u8 *. This would eliminate the need for typecasting.
For instance, in struct processor, *buffer is properly a pointer to struct record. If you then consistently referred to the structures in the parameter-lists of the various functions, "C" could detect inconsistencies for you. All bets are off when you use typecasting ("C" has no choice but to believe whatever you say is true).
That makes sense. I should be more consistent in passing structures and try to limit passing arbitrary types as parameters. I assume it's probably ok to typecast inside the function but keep it isolated in the function.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.