Connecting Tech Pros Worldwide Help | Site Map

Return a 2D array using pointers

Member
 
Join Date: Nov 2007
Location: Hyderabad, India.
Posts: 44
#1: 4 Weeks Ago
Hello

I need to call a function that returns a 2D array. I need to dynamically allocate memory for the array in the callee. Now, I return using a pointer to a pointer.

In the callee, I allocate the memory using mallloc, assign values to the array elements, output using printf in the callee. Eveything works fine.
But in the caller function, I am unable to access the array elements using the pointer returned. I either get garbage values, or an access violation error or both.

I am attaching the code. Will someone please tell me why it is not working?

Hanaa
Attached Files
File Type: txt arrays.txt (566 Bytes, 23 views)
Member
 
Join Date: Nov 2007
Location: Hyderabad, India.
Posts: 44
#2: 4 Weeks Ago

re: Return a 2D array using pointers


I am so very sorry.

I forgot to write the return statement in the callee.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,153
#3: 4 Weeks Ago

re: Return a 2D array using pointers


A semi decent modern compiler should issue a diagnostic about that which begs the questions

Are you using an old compiler or have you not switched on all warnings or did you ignore the warnings?

If it is the final case then never ignore warnings, they are there for a reason and 99% of the time you can make your code compile without warnings.
Reply