Skip to content

Instantly share code, notes, and snippets.

@johnhmj
Created January 1, 2010 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnhmj/267101 to your computer and use it in GitHub Desktop.
Save johnhmj/267101 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cstdlib>
#include <cstring>
#define BUFFERSIZE 256
// Integrated Development Environment
// Visual C++
using namespace std;
//
typedef struct DoublePointer
{
char** p;
// size of p
size_t size;
// Buffer Size of *p[]
size_t BufferSize;
}SDP;
void reverse(SDP& buffer, size_t size, char** str);
void DestroyDoublePointer(SDP& buffer);
void main(int argc, char** argv)
{
// Initialize double pointer of structure b
SDP b;
b.p = NULL;
b.BufferSize = BUFFERSIZE;
// Reverse and Allocate memory
reverse( b, argc, argv);
// display
for (size_t i = 0; i < b.size; i ++)
{
cout<<" "<<b.p[i];
}
cout<<endl;
// Free memory of double pointer
DestroyDoublePointer(b);
system("PAUSE");
}
void reverse(SDP& buffer, size_t size, char** str)
{
if ( buffer.p != NULL )
{
return;
}
// Ignore program name
buffer.size = size - 1;
// Allocate memory of p
buffer.p = (char**) calloc( buffer.size, sizeof(char*));
// Allocate memory of p[]
for (size_t i = 0; i < buffer.size; i ++)
{
buffer.p[i] = (char*)calloc( buffer.BufferSize, sizeof(char));
strcpy( buffer.p[i], str[size - i - 1]);
}
}
void DestroyDoublePointer(SDP& buffer)
{
if ( buffer.p ==NULL )
{
return;
}
// Free memory of p[]
for (size_t i = 0; i < buffer.size; i ++)
{
free(buffer.p[i]);
}
// Free memory of p
free(buffer.p);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment