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

Posted on 2006-04-29
Medium Priority
Last Modified: 2010-04-01
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.

Question by:twr999

Expert Comment

ID: 16569514
could u give us the prototype of GetData  ?

Author Comment

ID: 16569574
bool GetData (char**);
LVL 85

Expert Comment

ID: 16569637
   char **recs;
    recs = new char*[100];
    for( int i=0;i<100; i++ ){ recs[i]=new char[100]; }
    f.GetData ( recs );
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


Author Comment

ID: 16569782
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.
LVL 86

Assisted Solution

jkr earned 600 total points
ID: 16569802
How are you using/accessing/changing it inside 'GetData()'? I could imagine that passing it by reference, i.e.

void GetData(char**&);

could help.

Author Comment

ID: 16570011
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
      return true;

LVL 85

Accepted Solution

ozo earned 1400 total points
ID: 16570074
bool FileInput::GetData(char **&recs, int &count)

 char **recs; int count;
 GetData ( recs ,count );

Author Comment

ID: 16573090
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!

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question