[Solved] Dynamic Character Array – Stack


A working variation of my solution appears below. The reason I had to do it this way is because while I was able to dereference (**Array)[MAX_FILENAME_AND_PATHNAME_LEN] I was only able to modify the first string array in the array.

The string array was initialized and filled several strings. While I could reference a string contained within *Array[0] but was unable to reference any of the other strings. The resulting array will replace the original array. This method will only work in the initial code block where the array to be replaced is initialized.

#define MAX_FILENAME_AND_PATHNAME_LEN MAX_FILENAME_LEN + MAX_PATHNAME_LEN


/*

    This method was designed to free the memory allocated to an array.

*/
void FreeFileAndPathArrays( char (*Array)[MAX_FILENAME_AND_PATHNAME_LEN] )
{   
    free( Array );
}                 

/*

    This method was designed to remove an index from an array.  The result of this method will shrink the array by one.

*/
void Remove_Element( char (**ArrayPointer)[MAX_FILENAME_AND_PATHNAME_LEN],int Index, int *Array_Length, char (*Array)[MAX_FILENAME_AND_PATHNAME_LEN] )
{
    int i = 0;
    int j = 0;
    char String[MAX_FILENAME_AND_PATHNAME_LEN];    
    char (*NewArray)[MAX_FILENAME_AND_PATHNAME_LEN];
    char (*GC)[MAX_FILENAME_AND_PATHNAME_LEN];
    int Length = *Array_Length;
    int NewLength = Length - 1;
    size_t Size;
    
    NewArray = malloc( sizeof( *NewArray) *  NewLength * ( MAX_FILENAME_AND_PATHNAME_LEN ) );
    
    Size = sizeof( *NewArray ) * NewLength;
    memset(NewArray, 0, Size);
    UI_Display("Test Block:");
    
    for ( j = 0; j < NewLength; j++ )
    {
        if ( j != Index )
        {
            memcpy(String,Array[j],MAX_FILENAME_AND_PATHNAME_LEN);
            strcpy( Array[Index], String ); 
            Fill(NewArray,String,j);
            UI_Display(String);
        }
    }

    GC = Array;
    *ArrayPointer = NewArray;
    free(GC);          
    *Array_Length = *Array_Length - 1;
    
}

/*

    This method was designed to place a string into an index.

*/
void Fill( char (*Array)[MAX_FILENAME_AND_PATHNAME_LEN], const char * String, int Index)
{  
        strcpy( Array[Index], String ); 
}

/*

    This method was designed to place fill each string array contained within the array of string arrays with 0's.

*/
void PrepareFileAndPathArrays( char (*FullPathNames)[MAX_FILENAME_AND_PATHNAME_LEN], int ROWS )
{
    size_t Size;
    
    Size = sizeof( *FullPathNames ) * ROWS;
    memset(FullPathNames, 0, Size);
  
}

1

solved Dynamic Character Array – Stack