-2

i want to compare two QString Array with Qt and C++, i tried this, but it dosent work, (in fact my application crash...) :

if(ArrayOne[nb] != ArrayTwo[nb]){

}

And then i need to assign a value at an element of my array, but i dosent work too :

ArrayOne[nb] = ArrayTwo[nb];

To reach this :

if(ArrayOne[nb] != ArrayTwo[nb]){
ArrayOne[nb] = ArrayTwo[nb];
}

THANKS !

Random78952
  • 1,520
  • 4
  • 26
  • 36

2 Answers2

1

Your nb value is probably outside the range for the size of your QString. i.e., nb > ArrayOne.size-1.

Phlucious
  • 3,704
  • 28
  • 61
1

You say you have:

QString ArrayOne;

If this is true, then you are just comparing characters in two strings, not string arrays, at position nb. And QString's operator[] does not check range, so if nb is more than length of QString, program may very well crash, especially if you assign to invalid reference returned by QString::operator[]...

So, to be clear: your code probably crashes because nb >= ArrayOne.length()

hyde
  • 60,639
  • 21
  • 115
  • 176