0

I have a string with some spaces on the end. I would like to terminate this string at the position when the first space occurs, so that when I later do strncpy() on it, it would copy only the part of the string which doesn't contain spaces.

This is some try which gives me a segault obviously. How can I do what I intend to do?

int main() { char* s1 = "SomeString "; *(s1 + 10)='\0'; printf("%s\n",s1); return 0; } 
5
  • puts(strncpy(s2,s1,10)); will serve the purpose here. Commented Apr 26, 2015 at 11:11
  • 3
    The segfault is due to the fact that s1 points to a string which is allocated in read-only memory segment. Attempting to write into this segment causes a memory-access violation. That being said, you can use strtok for your purpose (again, as long as your string is allocated in a writable memory segment). Commented Apr 26, 2015 at 11:18
  • @naltipar: I'm not the down-voter, but I'd get rid of that 50 there. In addition, one good reason for the down-vote might be the fact that you've shown a very "hard-coded" example of how you truncate that string at index 10. Commented Apr 26, 2015 at 11:27
  • @naltipar: Most importantly, the segfault itself is not because OP has "not allocated space for your array". An array (or any piece of data for that matter) always has space allocated to it (how else would it exist?). The segfault here is due to the location of the array within the executable image (namely, the RO-data section instead of the stack). In fact, that is probably the main reason for the down-vote. Commented Apr 26, 2015 at 11:27
  • BTW: *(s1 + 10) is by definition equivalent to the more common s1[10]. It is also equivalent to *(10 + s1) and 10[s1] interestingly. Commented Apr 26, 2015 at 12:30

5 Answers 5

5

Modifying the content of a literal string like you do in *(s1 + 10)='\0'; is undefined behavior.

Sign up to request clarification or add additional context in comments.

Comments

3

You're attempting to modify a string that is read only because of the way you declared it pointing to a constant value. You'll have to make a copy first:

#include <stdio.h> #include <string.h> int main() { char* s1 = "SomeString "; char *s2 = strdup(s1); char *s3 = strchr(s2, ' '); *s3 = '\0'; printf("%s\n",s2); /* if not needed any more, because strdup allocates heap memory */ free(s2); return 0; } 

Comments

1

You can just use

char s1[] = "SomeString "; 

instead of

char* s1 = "SomeString "; 

Comments

0

A more generalized example where you actually find that first space character (if any). Also note the change in how s1 is defined to avoid the issue with literal strings (i.e. "this") possibly being stored in readonly memory. In your example s1 points directly at the literal string. Below the literal string is used to initialize the array s1.

 int main() { char s1[] = "SomeString "; char * spc = strchr(s1, ' '); if (spc != NULL) *spc = '\0'; printf("%s\n",s1); return 0; } 

Comments

-1

Maybe strstr could be useful in your case (http://www.cplusplus.com/reference/cstring/strstr/)

char * pFirstSpace = strstr (s1," "); if (pFirstSpace) *pFirstSpace=0; 

UPDATE

As many others have noticed, this turn out in Access Violation. This runs only on dynamic allocated chars. So you need to copy your string in a dynamic buffer before changes its content

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.