Article 6P6Y5 c: segfault while trying to assign a character to an element of a string.

c: segfault while trying to assign a character to an element of a string.

by
schneidz
from LinuxQuestions.org on (#6P6Y5)
i am essentially trying to remove \n from a string (if it exists).

Code:1:#include <stdio.h>
2:#include <string.h>
3:#include <stdlib.h>
4:
5:int main(int argc, char *argv[])
6:{
7: char *ex1 = "123 hello world\n";
8: char *pp;
9: int i1;
10:
11: i1= strtol(ex1, &pp, 10);
12: printf("ex1 = %s: i1 = %d -- pp = \"%s\" -- strcspn-pp = %d\n",ex1,i1,pp,strcspn(pp,"\n"));
13:// pp[strcspn(pp, "\n")] = '\0';
14: pp[6] = 'a';
15: printf("\n\n-----apres call: pp = \"%s\"\n\n\n", pp);
16:}Code:~ $ gcc storage/shared/stuff/schneidz-test.c && ./a.out
ex1 = 123 hello world
: i1 = 123 -- pp = " hello world
" -- strcspn-pp = 12
Segmentation faultif i comment out line 14 it will print the same pp as in line 12.
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments