How do I use an array of pointers to char as a function parameter?

Hello all,
I'm new to C++ and having some trouble getting my mind wrapped around the whole pointers and references thing.

I need to be able to call a function that loads data from a file into an array of pointers to type char. (I think I'm describing that correctly -- it's an array of c-style strings.) What is the correct way to declare the array in the calling function, and how do I pass it as a parameter to the function that loads the data?

The function that loads the data declares it as char**, and the debugger shows this to be working fine.

I've tried declaring it as "char** recs" in the calling function, and then passing it as just "recs":
   
   f.GetData ( recs );

but the caller is getting back 0.

I doubt that this is difficult for someone with C++ experience, but I'd really like some help soon, so I'm assigning the max points.

Thanks.
twr999Asked:
Who is Participating?
 
ozoCommented:
bool FileInput::GetData(char **&recs, int &count)

 char **recs; int count;
 GetData ( recs ,count );
0
 
MechanTOurSCommented:
could u give us the prototype of GetData  ?
0
 
twr999Author Commented:
bool GetData (char**);
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
ozoCommented:
   char **recs;
    recs = new char*[100];
    for( int i=0;i<100; i++ ){ recs[i]=new char[100]; }
    f.GetData ( recs );
0
 
twr999Author Commented:
I think I failed to ask the question clearly.

My initialization code, which looks just like the second and third lines of ozo's code is in the GetData function. The declaration of the array (first line of ozo's code) is in the calling function.

Inside GetData, the data is being loaded with no problems. When GetData returns, the caller sees recs as 0. What I need to know is how to pass the recs array to GetData so that I can use it (access the data) after the call to GetData returns.
0
 
jkrCommented:
How are you using/accessing/changing it inside 'GetData()'? I could imagine that passing it by reference, i.e.

void GetData(char**&);

could help.
0
 
twr999Author Commented:
I tried the char**& but got a syntax error.

Inside the GetData function, I initialize the array with new and then load it with data. (btw, if it matters, It's MS Embedded VC++)  There's a record at the beginning of the file that contains 4 characters indicating how many data data records the file contains (not my design -- my task is just to figure out how to read it.) Here's the function:

bool FileInput::GetData(char **recs, int &count)
{
      unsigned long i = 0;
      BYTE buffer[REC_LEN];
      BYTE strSize[4];              
      int size, c;                                              
      
      // get the first (size) record
      ReadFile ( hFile, strSize, 4, &i, NULL );  
      size = atoi( (char*)strSize );              
      
      // initialize the array to [size][REC_LEN]
      recs = new char*[size];
      for (c=0;c<size;c++)
            recs[c] = new char[REC_LEN];

      // get the data into the array
      for (c=0;c<size;c++)
      {
            ReadFile( hFile, buffer, REC_LEN, &i, NULL);
            recs[c] = new char[REC_LEN];
            strcpy(recs[c], (char*) buffer);
      }
      count = c;   // return data to caller
      
      CloseHandle(hFile);
      return true;
}


0
 
twr999Author Commented:
I split the points on this, because both jkr and ozo had the answer, but I gave more to ozo because that answer had enough information for me to get the code working. Thanks to everyone who helped out!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.