Hello folks,
I have three interrelated questions. I'm sorry if these are newbie questions, but I can't seem to find these answers in online searches.
- can one declare several pointers in main(), pass these pointers as arguments to a function, point these pointers to malloc'd memory inside this function, and still access this memory in main() after the function returns? An initial experiment suggested that this is illegal, which I interpret to mean that declaration of a pointer and the memory allocated to that pointer each have individual scopes and that the scope of the allocated memory does not inherit the scope of the pointer.
- can one define a pointer to an array "above main", then allocate the memory for it in a function that is called from within main() and still have the memory be accessible to main after the function returns? I suspect based on what I interpreted from the above experiment that the answer is no.
- If a pointer to an array is declared above main and malloc'd at the top of main, is the memory assigned to the array visible within all functions that are called later from within main?
Many thanks,
kris
I have three interrelated questions. I'm sorry if these are newbie questions, but I can't seem to find these answers in online searches.
- can one declare several pointers in main(), pass these pointers as arguments to a function, point these pointers to malloc'd memory inside this function, and still access this memory in main() after the function returns? An initial experiment suggested that this is illegal, which I interpret to mean that declaration of a pointer and the memory allocated to that pointer each have individual scopes and that the scope of the allocated memory does not inherit the scope of the pointer.
- can one define a pointer to an array "above main", then allocate the memory for it in a function that is called from within main() and still have the memory be accessible to main after the function returns? I suspect based on what I interpreted from the above experiment that the answer is no.
- If a pointer to an array is declared above main and malloc'd at the top of main, is the memory assigned to the array visible within all functions that are called later from within main?
Many thanks,
kris
Comment