• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

initializing pointers within functions in C/C++

Special K

Diamond Member
I have some pointers that I set to NULL at the beginning of main(). They are later initialized to point to something meaningful within another function. The problem is, when I return to main(), they are still pointing to NULL. How can I make it so that my pointer assignments that I make in the function are preserved when the scope returns to main?

Here I have shown the function declaration and me trying to use it:

 
Well I ended up just making a pointer to the pointers I wanted to modify, and then dereferencing them within the function to modify the original ones. I guess that is OK?
 
Originally posted by: Special K
Well I ended up just making a pointer to the pointers I wanted to modify, and then dereferencing them within the function to modify the original ones. I guess that is OK?

that's the proper way to do it, along with alternatives like using references (C++ only) or simply returning a pointer
 
Originally posted by: dighn
Originally posted by: Special K
Well I ended up just making a pointer to the pointers I wanted to modify, and then dereferencing them within the function to modify the original ones. I guess that is OK?

that's the proper way to do it, along with alternatives like using references (C++ only) or simply returning a pointer

How do I pass an array of pointers (char**) by reference? What would my function prototype look like? Don't you use the apersand (&) to do it?

 
Back
Top