ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
When you call 'another_func', the pointer 'ptr' from 'some_func' is passed by value. This means that the local variable 'ptr' is a copy of the value of the 'ptr' that was passed to it from 'some_func'. When you call 'malloc', you are reassigning the local version of 'ptr' to a new address, but since it is a copy, the 'ptr' in 'some_func'/'main' that was passed to it remains unchanged. Try changing the argument type of 'ptr' in your functions to 'char*&' instead of just 'char*'.
Sorry, I was a little unclear. The addition of '&' makes the argument a reference to a pointer. This means that the value of the 'ptr' varable passed to the functions can be changed. It is just like having your argument type 'char**', except that you don't have to dereference it to change the value. Also, you need to use the 'free' function somewhere in there to deallocate the memory allocated by malloc.
void another_func(char **ptr)
*ptr = malloc(4); //<- reassigns the pointer passed to another_func
void another_func(char *&ptr)
ptr = malloc(4); //<- reassigns the variable passed to another_func
I can make the void some_func(char **ptr) work, and just dereference when in use. I still can't get the void another_func(char *&ptr) (I assume you mean '*&' within the function prototype right?) syntax to work. The compiler gives the same error. The *& seems to be the best solution, because you don't have to dereference the variable every time. Could you alter the eksisting code, with the '*&' example, so I can see prototypes and function calls. Thanks
Didn't see last 2 post. Makes sense then. The '*&' is for C++ then?
Does the ** pointer notation not apply to structs (would think of a char as a struct too, I would think)? I've tried to do a similar test code, because I wanted to pass all my var's this way, but it doesn't seem to recognize my parameter as a pointer to my struct.