Hi,
I'm new to pointers. i got some requirements......
Following is my requirement.........i want to use a double and triple pointer array [2D,3D] and in that double pointer i want to get data from the user.how can i achieve that using malloc and free for assigning and deallocating memory.
im using vc++ 6....
//#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#define FIRSTDIMENSION 10
#define SECONDDIMENSION 20
#define THIRDDIMENSION 30
bool Allocate (char* c)
{
c = (char*) malloc (FIRSTDIMENSION);
if (c !=NULL)
{
printf ("\n Here in Allocate (char* c) Success \n\n ");
return true;
}
else
{
printf ("\n Here in Allocate (char* c) Failre \n\n ");
return false;
}
}
//2D Array
//Want to allocate memory for double pointer as 10*20 array
bool Allocate (char** c1)
{
c1 = (char**) malloc (FIRSTDIMENSION);
//how to allocate memory for double pointer????
for ( int nValue = 0; nValue < SECONDDIMENSION; nValue++)
{
c1[nValue] = (char*) malloc ( SECONDDIMENSION * sizeof(char*) );
cin>>c1[nValue];
}
//Here i'm trying to get value from input
/*for(int i =0; i<3;i++)
for(int j=0; j<2;j++)
cin>>c1[i][j];
*/
//Trying to print value in the pointer
for ( nValue = 0; nValue < SECONDDIMENSION; nValue++)
{
cout<<c1[nValue]<<endl;
}
if (c1 !=NULL)
{
printf ("\n Here in Allocate (char** c) Success \n\n ");
return true;
}
else
{
printf ("\n Here in Allocate (char** c) Failre \n\n ");
return false;
}
return true;
}
//3D Array
bool Allocate (char*** c2)
{
//i have no idea how to allocate memory for 3D pointer
c2 = (char***) malloc (FIRSTDIMENSION);
//how to get value for a triple pointer and print the value in triple pointer
if (c2 !=NULL)
{
printf ("\n Here in Allocate (char*** c) Success \n\n ");
return true;
}
else
{
printf ("\n Here in Allocate (char*** c) Failre \n\n ");
return false;
}
return true;
}
void Release ( char* c)
{
free (c);
c = NULL;
}
void Release ( char** c1)
{
free (c1);
c1 = NULL;
}
void Release ( char*** c2)
{
free (c2);
c2 = NULL;
}
int main(int argc, char* argv[])
{
printf("Hello World!\n");
char* cValue = NULL;
char** cValue1 = NULL;
char*** cValue2 = NULL;
if (false == Allocate (cValue))
{
printf ("\n Allocate (char* c) failed \n\n ");
}
if (false == Allocate (cValue1))
{
printf ("\n Allocate (char** c) failed \n\n ");
}
if (false == Allocate (cValue2))
{
printf ("\n Allocate (char*** c) failed \n\n ");
}
Release (cValue);
Release (cValue1);
Release (cValue2);
return 0;
}