Re: SIGSEGV error received while implementing string functions

  • From:
  • To:
  • Subject: Re: SIGSEGV error received while implementing string functions
  • Date: Fri, 18 Jan 2013 10:18:17 +0100 (CET)


----- Original Message -----
> 
> acsabir wrote:
> > Hi,
> > 
> > Code:
> > #include "strings.h"
> >     char *s1;
> >     s1 = "   ali ata bak.   ";
> 
> a = "aaa" is invalid code in C

Hi

I'd say that the assignment is legal in C (though not in C++, s1 would have 
to be const), but modifying the contents of the string literal is undefined. 
However, such an assignment only copies the _pointer_ (in this case, a 
pointer to the address of the literal string is taken). The contents of the 
string are not copied.

You need to write something like

s1 = strdup("   ali ata bak.   ");

(and free it)

in order to be able to modify the contents of the string.

Compare this to C++ using std::string:

// s1 contains a copy of the literal string, and can be modified
std::string s1("   ali ata bak.   ");

A+
Paul


SIGSEGV error received while implementing string functions

acsabir 01/17/2013

SIGSEGV error received while implementing string functions

soldatov 01/18/2013

Re: SIGSEGV error received while implementing string functions

paulf 01/18/2013

SIGSEGV error received while implementing string functions

acsabir 01/20/2013

Re: SIGSEGV error received while implementing string functions

Paul Floyd 01/20/2013

Project Features

About this Project

CND was started in November 2009, is owned by Leonid Lenyashin, and has 130 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close