GCC + pointer increment = Seg fault !

EmperorRob

Senior member
Mar 12, 2001
968
0
0
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?
 

EmperorRob

Senior member
Mar 12, 2001
968
0
0
I take that back. The problem is trying to assign a char to a pointer like this:

*string = letter;
 

Ameesh

Lifer
Apr 3, 2001
23,686
1
0
try adding parenthesis, it might be the linux ggc has fvcked up operator precedence
 

EmperorRob

Senior member
Mar 12, 2001
968
0
0
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.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0

.
.
*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";
.
 

EmperorRob

Senior member
Mar 12, 2001
968
0
0
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.
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0

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?
 

manly

Lifer
Jan 25, 2000
13,086
3,850
136
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
 

EmperorRob

Senior member
Mar 12, 2001
968
0
0
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.
 

manly

Lifer
Jan 25, 2000
13,086
3,850
136
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.