• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

GCC + pointer increment = Seg fault !

EmperorRob

Senior member
I have been working with cc on a UNIX system. And this simple program comiles and runs fine:

------------------------------------------START-----------------------------------
#include <stdio.h>

char * strset(char * original, char letter);

main() {
char * word = "Rob";
char letter = 'x';
printf("%s\n", strset(word, letter));
}

char * strset(char * string, char letter) {
char * original = string;

while (*string)
*string++ = letter;
return(original);
}
------------------------------------END-------------------------------------

The problem is when I move this same program over to a Linux machine and use gcc it compiles fine but when I try to execute it, it Segmentation faults.

I've narrowed the problem down to this line:
*string++ = letter;

gcc will not let me increment a pointer. Does anyone know a way to do this?
 
Yeah I tried that with no luck. Then I tried this syntax too:

for (j = 0; word[j]; j++)
word[j] = letter;


No luck in either case. It always craps out when I try to assign a specific character another char.
 

.
.
*string++ = letter;
.


you are trying to modify a const qualified string, which raises a sigsev on a write. there's nothing wrong with the ++ operator. to eliminate the problem try doing something like this ( oh and there's nothing wrong with gcc )


.
char word[] = "Rob";
.
 
What I'm trying to do is convert all the letters of a string to 'x'. And do this on a pointer to a char array instead of just a char array. In other words

This: char * word
not: char word[]

I don't understand why that syntax will work in cc but not gcc.
 

char* word = strdup("Rob");
memset(word, 'x', strlen(word));
free(word);


what version of UNIX are you using ( Solaris, HP-UX, ... )? and how old is the cc on that box?
 
Like HigherGround said, you can't modify a string constant declared by a pointer. You can if you allocated a char array holding that sequence of characters.

This is covered specifically in K&R 2e page 104, Sect. 5.5. If you could only have one C language book in your library, this is the one. 🙂

Also in Stroustrup The C++ Programming Language 3e page 90, Sect. 5.2.2
 
The UNIX box is TRU64 4.0.

(cc)Digital UNIX Compiler Driver 3.11

The linux machines are Red Hat 6.2-7.2

I think I understand the constant string. But what if I had a declaration like this:
char * word = substr(str2, 1, 10)

Would my function work or would that still be considered a string constant?

manly, where can I get that book? I'm looking for help mainly on UNIX/LINUX programming. Thanks people.
 
The C Programming Language, usually referred to as "K&R" for its authors, the creators of C. It's a concise tutorial and reference on the C language.

EmperorRob, think of it this way. When you declare a pointer to a string constant, you don't own the underlying storage to that string. Therefore, it's undefined what happens when you try to modify that underlying buffer. Even if it happens to work on one compiler/machine, the code itself is incorrect.
 
Back
Top