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;
}

Recommended Answers

All 2 Replies

is the following declaration correct for 2D and 3D pointer

2D pointer memory allocation

//temp1 is a double pointer
//FIRST_DIMENSION = 10;SECOND_DIMENSION = 20
//is this declaration correct for a 10*20 2D array
temp1=(char**)malloc (FIRST_DIMENSION * sizeof(char *));
for(int nvalue=0; nvalue<FIRST_DIMENSION; nvalue++)
    temp1[nvalue] = (char *) malloc(SECOND_DIMENSION * sizeof(char));
//getting input from the user
for(int i =0;i<FIRST_DIMENSION;i++)
   for(int j=0;j<SECOND_DIMENSION;j++)
   { 
    cout<<"Enter the value for 2D array\n";
    cin>>temp1[i][j];
   }

3D pointer memory allocation

//temp2 is a triple pointer
//FIRST_DIMENSION = 10;SECOND_DIMENSION = 20;THIRD_DIMENSION = 30
//is this declaration correct for a 10*20*30 3D array
temp2=(char***)malloc (FIRST_DIMENSION * sizeof(char *));
for(int i =0;i<SECOND_DIMENSION;i++)
  {
   temp2[i] = (char**) malloc(SECOND_DIMENSION * sizeof(char *));
   for(int j=0;j<THIRD_DIMENSION;j++)
   {
    temp2[i][j] = (char *) malloc(SECOND_DIMENSION * sizeof(char));
   }
  }
//getting input from user
for(i =0;i<FIRST_DIMENSION;i++)
   for(int j=0;j<SECOND_DIMENSION;j++)
   for(int k=0; k<THIRD_DIMENSION;k++)
   {
   cout<<"Enter the value for 3D array\n";
   cin>>temp2[i][j][k];
   }

You are missing one star at line 4 of the second code snippet:

temp2=(char***)malloc (FIRST_DIMENSION * sizeof(char **));

For the rest, it is the correct way to do it.

BTW sizeof(char) is always equal to 1, by definition from the standard.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.