Vector functions

I desperately need some help here as there is something that's driving me nuts! I just can't explain a certain code behavior.

I have this program:

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
vector<string> function(int myint, string mystring)
{		
       int K = mystring.length();
       //char array1[K];
       char line[K];
       vector<string> myreturn;
        
		
	for (int i=0; i<K; i++)
        {
                    if(mystring[i]=='.')
                    {
                            line[i]='.';
                     }
                    else
                           line[i]='X';
       }

        myreturn.push_back(line);
      
       return myreturn;
}   

int main(int argc, char *argv[])
{
       string my_string="QQQQ";
       cout<<function(2,my_string)[0]<<endl;
       system("PAUSE");
}


AS EXPECTED, it prints XXXX.
So far so good.

Now, I make a little modification. I add a single line ( char array1[K]; ) within the function body.Basically I activate the deactivated line above so the code looks like

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
vector<string> function(int myint, string mystring)
{		
       int K = mystring.length();
       char array1[K];
       char line[K];
       vector<string> myreturn;
        
		
	for (int i=0; i<K; i++)
        {
                    if(mystring[i]=='.')
                    {
                            line[i]='.';
                     }
                    else
                           line[i]='X';
       }

        myreturn.push_back(line);
      
       return myreturn;
}   

int main(int argc, char *argv[])
{
       string my_string="QQQQ";
       cout<<function(2,my_string)[0]<<endl;
       system("PAUSE");
}


Now, after I have declared one more array the program output is all over the place. Instead of XXXX I get some very weird stuff.

ANY HELP/SUGGESTIONS?
Last edited on
I wonder you were able to compile your code at all.

You can't declare an array with a non-const size (line 5 - char line[K];). In such a case the size of the array is not known to the compiler during compilation-time so it should print an error message and refuse to build your program.
makes sense. problem is my compiler didn't say a thing.

So what am I supposed to do if I DO need to use an array of size[K], where K is uknown (dependent on the function input)?

Given that you are passing the string to be checked to the function() function by value, then you can brutalize that copy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 vector<string> function(int myint, string mystring)
{		
       vector<string> myreturn;
        
		
	for (int i=0; i<mystring.length(); i++)
        {
            if (mystring[i] != '.')
                mystring[i] = 'X';
               
       }

        myreturn.push_back(mystring);
      
       return myreturn;
}   



I won't ask why you are making a vector just to hold one string.
Last edited on
good question about the why vector.

The function is a much more complicated version of this one. I had to cut it down to as small as possible, but still contain the main idea, so I can track down the problem and post it here as well.

Good approach you propose, and neat, and it would work beautifully if the actual problem was as in the code.

BUT as this is part of a bigger problem I actually do need a couple of arrays of length K to use in the code, and compare with each other, at the same time.

Any other way around this comes to mind?

Last edited on
SOLVED! you can use strings and .append() to build your arrays from the ground up.

Danielsson wrote:
You can't declare an array with a non-const size (line 5 - char line[K];). In such a case the size of the array is not known to the compiler during compilation-time so it should print an error message and refuse to build your program.

GCC will let you do this but for what reason I do not know.
Topic archived. No new replies allowed.