Passing a string to another function

I'm trying to pass a string as a pointer to another function and getting some errors. I need a template array that always get's reset after I've made changes and published the changes, so I was going to use a pointer passed to another function. Here's an example of what I'm trying to do.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <stdio.h>
#include <string>

using namespace std;

void reset_myString( string *szTemp[5] );

int main ()
{
  int x;
  string szTemp[5];
  reset_myString( szTemp[5] );

  for ( x = 0; x <= 5; x++ ) { cout << szTemp[x] << endl; }
  szTemp[3] = "BREAK";
  for ( x = 0; x <= 5; x++ ) { cout << szTemp[x] << endl; }
  reset_szTemp( &szTemp[5] );
  for ( x = 0; x <= 5; x++ ) { cout << szTemp[x] << endl; }

  return 0;
}

void reset_myString( string *szTemp[5] )
{
  for ( int x = 0; x <= 5; x++ )
    szTemp[x].erase();

  szTemp[0] = "abc";
  szTemp[1] = "def";
  szTemp[2] = "ghi";
  szTemp[3] = "jkl";
  szTemp[4] = "mno";
  szTemp[5] = "pqr";
}


Error: cannot convert `std::string' to `std::string**' for argument `1' to `void reset_myString(std::string**)'|
Here's a fixed version with the changes noted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>   //// you had stdio.h
#include <string>
using namespace std;

void reset_myString( string* szTemp );  //// array size not needed

int main ()
{
  int x;
  string szTemp[6];  //// must be 6 to use elements 0 through 5
  reset_myString( szTemp );   //// don't need the array size here

  for ( x = 0; x <= 5; x++ ) { cout << szTemp[x] << endl; }
  szTemp[3] = "BREAK";
  for ( x = 0; x <= 5; x++ ) { cout << szTemp[x] << endl; }
  reset_myString( szTemp ); //// same as above
  for ( x = 0; x <= 5; x++ ) { cout << szTemp[x] << endl; }

  return 0;
}

void reset_myString( string *szTemp )
{
  for ( int x = 0; x <= 5; x++ )
    szTemp[x].erase();

  szTemp[0] = "abc";
  szTemp[1] = "def";
  szTemp[2] = "ghi";
  szTemp[3] = "jkl";
  szTemp[4] = "mno";
  szTemp[5] = "pqr";
}
Topic archived. No new replies allowed.