Tuesday, January 22, 2013

Char Pointer : Segmentation Fault



#include<iostream>
using namespace std;
int main()
{
        //value will be in read only area of data segment
        char *str = "abc";
        cout<<"Str = "<<str<<endl;
        str[1] = 'x';// This will give segmentation fault
        cout<<"Str = "<<str<<endl;
}

PS: char str[] = "abc"; will be in stack hence can be modified.

No comments:

Post a Comment