How do I create a function that accepts by refrence an array of character pointers?

I need to be able to overwrite the contents of an array of character pointers, hence I need a pointer to that array.

Here is some of my code, but it is throwing errors, so will be overwriting it:

//function prototype. 
void getFileNames(char **contents[]);

//in main
char *contents[] = {"Hello", "World!!!"};

//function definition
void getFileNames(char **contents[]){}

Maybe there is a better way of doing this.

3 Years
Discussion Span
Last Post by Unimportant
void getFileNames( char ** fileNames ); // This is a function which receives a pointer to char pointer
const char * someFile = "Hello"; // this is a const char pointer to the start of Hello
const char ** someFiles = { "Hello", "World" }; // This is a const char pointer to an array of const char pointers
char ** fileNames = new char *[8]; // This is 8 char pointers
for(int i=0;i<8;++i) fileNames[i] = new char[MAX_LEN] ; // Each char pointer is given its own pool of memory
sprintf( fileNames[0], "%s.jpg", "image" ); // Write to the first and
sprintf( fileNames[1], "%s.jpg", "8184859" ); // second char pointers
getFileNames( fileNames ); // Pass the pointer to pointers to getFileNames
for(int i=0;i<8;++i) delete [] *(fileNames + i); // Delete strings
delete [] fileNames; // Delete pointers to strings

Edited by Unimportant


I think I will just use a queue. It looks like there is no way to do what I am trying to do, especially when arrays are passed by refrence in C, and what I need is not a refrence, but a pointer that can be changed.


to begin with, you're trying to modify a const string,

const char * str = "Hello world";
str[0] = "J"; // This is totally absurd.

You actually want a block of memory just for pointers...

    char ** strings;
    int number_of_strings( 8 ), length_of_string( 260 );
    strings = (char**)malloc( number_of_strings * sizeof( char * ) ); 
    for(int i=0;i<8;++i) strings[i] = (char*)malloc( length_of_string );
    // Now you have strings[8][260]
    strcpy( strings[0], "Hello" );
    strcpy( strings[1], "World" );
    // Now strings[0] and strings[1] are valid

    // How you pass things doesn't matter, don't be absurd
    void acceptStrings( DWORD strings )
      char ** passed( (char**)strings );
      printf( "%s %s", passed[0], passed[1] );
    } // This is vaild. A pointer is a regular number.
    acceptStrings( (DWORD)&strings );

    // Clean up the memory when you're finished.
    for(int i=0;i<8;++i) free( *(strings+i) );
    free( strings );

Edited by Unimportant

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.