dynamic array problem odd

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
35
36
//ok so I made my own namespace with a function:


namespace pwsRegEx{



char Regex_Replace(char * arrpt[],char * arrptreplace[],char * source){
int i_arrpt,i_arrptreplace;
//for(i_arrpt=0;arrpt[i_arrpt];i_arrpt++); 

cout<<arrpt[3]<<endl;

}          



}



//The code i'm using under my app procedure under WM_NOTIFY:

case WM_NOTIFY:
             {
char * regsource="adsasd\"asdasd\"asdads"; 
char * pattern_replace[]={"<font color=red>< ?php</font>","MySQL_query(<font color=green>$1</font>)"};
char * pattern[]={"/< \\?php/","/MySQL_query\\((.*)\\)"};


             char * mike[7]={"aa","bb","cc","dd","ee","ff","gg"};


             Regex_Replace(pattern,pattern_replace,regsource);
             }



now here's the odd thing which doesn't make any sense to me what so ever.

under my namespace pwsRegEx::RegexReplace();

I use cout to output:

cout<<arrpt[0]<<endl; //outputs: /< \\?php/ Correct
cout<<arrpt[1]<<endl; //outputs: /MySQL_query\\((.*)\\) Correct
cout<<arrpt[2]<<endl; //outputs: <font color=red>< ?php</font> Not correct this is in another array
cout<<arrpt[3]<<endl; //outputs: "MySQL_query(<font color=green>$1</font>) Again same as before not correct
cout<<arrpt[4]<<endl; //outputs: Giberish text and i have no idea why i'm seeing this



so heres the thing.



if I'm asking for data from one array then how is data from another array ending up in my first array which is arrpt?

arrpt is displaying data from arrtptreplace which it should not so why is it doing that?

Thanks and I never encounter this problem before :/
gcc wrote:
warning: deprecated conversion from string constant to 'char*'
AFAIK, if you declared them in that way they are
const char *

That and out of bounds
pattern[]={"/< \\?php/","/MySQL_query\\((.*)\\)"}; ¿How many elements holds the array?
Last edited on
ok well I tried using const char * and I still have the same results. Why is it accesssing data from another array once I go out of bounds?

For example I just tried:

1
2
3
4
5
6
7
char Regex_Replace(const char * arrpt[],const char * arrptreplace[],const char * source){
int i_arrpt,i_arrptreplace;
//for(i_arrpt=0;arrpt[i_arrpt];i_arrpt++); 

cout<<arrpt[3]<<endl;

} 


1
2
3
4
5
6
7
8
9
const char * regsource="adsasd\"asdasd\"asdads"; 
const char * pattern_replace[]={"<font color=red>< ?php</font>","MySQL_query(<font color=green>$1</font>)"};
const char * pattern[]={"/< \\?php/","/MySQL_query\\((.*)\\)"};


             const char * mike[7]={"aa","bb","cc","dd","ee","ff","gg"};


             Regex_Replace(pattern,pattern_replace,regsource);


and its outputting :

MySQL_query(<font color=green>$1</font>)

I should only be seeing that if I used arrptreplace not arrpt


I know I still have alot to learn on C++ and I can only make basic Windows apps but why are the arrays conflicting with one another?

Thanks
Last edited on
pattern is an array of 2 elements. You are trying to access the fourth one (remember that is zero based)
That is an out of bounds error, and causes undefined behaviour.
ok so then your saying don't go out of bounds and all which I understand. The problem is how can I get the size of array without using vector?

I know that for(i_arrpt=0;arrpt[i_arrpt];i_arrpt++); is wrong and bad practice last I checked.

and if I do have to use vector then how would I be able to since I never used it before?

Thanks
ok so I added two more parameters that will pass the size of the array to the function like so:

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
namespace pwsRegEx{
#define ArrSize(x) (sizeof (x) / sizeof *(x))


char Regex_Replace(const char * arrpt[],int arrptsize,const char * arrptreplace[],int arrptreplacesize,const char * source){

cout<<"Pattern size:"<<<<endl;                                        
}          
}


case WM_NOTIFY:
             {
const char * regsource="adsasd\"asdasd\"asdads"; 
const char * pattern_replace[]={"<font color=red>< ?php</font>","MySQL_query(<font color=green>$1</font>)"};
const char * pattern[]={"/< \\?php/","/MySQL_query\\((.*)\\)"};

const char * mike[7]={"aa","bb","cc","dd","ee","ff","gg"};
const char *arr[] = {"Hello,", "my", "name", "is", "Pax."};
cout<< "arr::::::"<< sizeof (arr) / sizeof (*arr)<<endl; 

Regex_Replace(pattern,ArrSize(pattern), pattern_replace, ArrSize(pattern_replace),regsource);
}

...



That seems to work ok as far as I can see


Now I have something else in mind if its possible.


when you declare your functions is there anyways I can do something similar to this:?

 
char Regex_Replace(const char * arrpt[],int ArrSize(arrptsize),const char * arrptreplace[],int ArrSize(arrptreplacesize),const char * source){


so this way I don't always have to specify ArrSize every time I use so it does it for me automatically.

of course I tried that and got errors and I figured I would


So would this be possible some how?

Thanks
Using vector
char Regex_Replace(vector<string> &arrpt,vector<string> &arrptreplace, string source);
The vectors know their own size. arrpt.size();

If you work with pointers, you could use a sentinel (like terminating the arrays with NULL or "").
Topic archived. No new replies allowed.