help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Varför segmenteringsfel?


From: Erik Sigra
Subject: Varför segmenteringsfel?
Date: Fri, 13 Jul 2001 16:57:48 +0200

pointers.cc:
---------------------------------------------
#include <iostream>

int main() {
  char * a = "xy";
  char * b = a + 1;
  cout
    << "int(a) = " << int(a) << "  int(b) = " << int(b) << endl
    << "*a = " << *a << "  *b= " << *b << endl
    << "a = " << a << "  b = " << b << endl;
  *a = *b;
}
---------------------------------------------

The intention is that the string should be changed from "xy" till "yy", but 
it ends like this:

$ make && ./pointers
g++ -Wall -Woverloaded-virtual -ansi -pedantic -O pointers.cc -o pointers
chmod +x pointers
int(a) = 134558664  int(b) = 134558665
*a = x  *b = y
a = xy  b = y
Segmentation fault


How should I do?



reply via email to

[Prev in Thread] Current Thread [Next in Thread]