By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,499 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,499 IT Pros & Developers. It's quick & easy.

A strange string function error

P: n/a
I have wrote a small function but got the error
function:
/*< squeeze */
void squeeze(char s1[],char s2[])
{
int de_s ;
int i,j,k;

for(i = 0;s2[i] !='\0';++i)
{
de_s = s2[i];

for(j = k = 0;s1[j] != '\0';++j)
{
if(s1[j] != de_s)
{
s1[k++] = s1[j];
}
}
s1[j] = '\0';
}
//printf("s1:%c",s1[0]);
}
/*>*///end-squeeze
the function just deletes each character in s1 that matches any
character in the string s2.(exercise from the c language
programming).But it doesn't work and I got
the message form GDB:
Starting program: /home/jack/myPro/myC/ListTest/CprogrammingTest/
Char01/main

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400659 in squeeze (s1=0x40098b "abcdefb", s2=0x400988
"ab") at main.c:40

I use squeeze("abcdefb","ab") to call the function.
Any suggestion?
Thanks :)
Nov 7 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Nov 7, 6:55*pm, "jackl...@gmail.com" <jackl...@gmail.comwrote:
I have wrote a small function but got the error
function:
/*< squeeze */
void squeeze(char s1[],char s2[])
{
* * * * int de_s ;
* * * * int i,j,k;

* * * * for(i = 0;s2[i] !='\0';++i)
* * * * {
* * * * * * * * de_s = s2[i];

* * * * * * * * for(j = k = 0;s1[j] != '\0';++j)
* * * * * * * * {
* * * * * * * * * * * * if(s1[j] != de_s)
* * * * * * * * * * * * {
* * * * * * * * * * * * * * * *s1[k++] = s1[j];
* * * * * * * * * * * * }

* * * * * * * * }
* * * * * * * * s1[j] = '\0';

* * * * }
* * * * //printf("s1:%c",s1[0]);

}

/*>*///end-squeeze
the function just deletes each character in s1 that matches any
character in the string s2.(exercise from the c language
programming).But it doesn't work and I got
the message form GDB:
Starting program: /home/jack/myPro/myC/ListTest/CprogrammingTest/
Char01/main

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400659 in squeeze (s1=0x40098b "abcdefb", s2=0x400988
"ab") at main.c:40

I use squeeze("abcdefb","ab") to call the function.
Any suggestion?
Thanks :)
why "int de_s ;" but not "char des_s;"?
Nov 7 '08 #2

P: n/a
ja******@gmail.com wrote:
I have wrote a small function but got the error
function:
/*< squeeze */
void squeeze(char s1[],char s2[])
{
int de_s ;
int i,j,k;

for(i = 0;s2[i] !='\0';++i)
{
de_s = s2[i];
(Why not declare `de_s` here, rather than in the outer loop?)

(Why is it `int`, not `char`?)

(Why is it called `de_s`?)
>
for(j = k = 0;s1[j] != '\0';++j)
{
if(s1[j] != de_s)
{
s1[k++] = s1[j];
You're writing into the string `s1`.
}
}
s1[j] = '\0';
}
//printf("s1:%c",s1[0]);
}
/*>*///end-squeeze

I use squeeze("abcdefb","ab") to call the function.
You pass the string literal `"abcdefb"` as the value for the
argument `s1`.

You are not allowed to write into string literals.

You were lucky: BOOM.
Any suggestion?
Don't write into string literals, and don't use so many blank lines
and whitespace in your code.

void squeeze(char s1[],char s2[])
{
int i,j,k;
for(i = 0; s2[i] !='\0'; ++i)
{
char de_s = s2[i];
for(j = k = 0; s1[j] != '\0'; ++j)
{
if(s1[j] != de_s) s1[k++] = s1[j];
}
s1[j] = '\0';
}
//printf("s1:%c",s1[0]);
}

I won't comment on the algorithm itself.

--
"Possibly you're not recalling some of his previous plans." Zoe, /Firefly/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

Nov 7 '08 #3

P: n/a
ja******@gmail.com wrote:
I have wrote a small function but got the error
function:
/*< squeeze */
void squeeze(char s1[],char s2[])
{
<snip>
}
/*>*///end-squeeze
the function just deletes each character in s1 that matches any
character in the string s2.(exercise from the c language
programming).But it doesn't work and I got

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400659 in squeeze (s1=0x40098b "abcdefb", s2=0x400988
"ab") at main.c:40

I use squeeze("abcdefb","ab") to call the function.
Any suggestion?
Don't pass string literals to functions that expect modifiable arguments.

--
Ian Collins
Nov 7 '08 #4

P: n/a
On Nov 7, 7:21 pm, Chris Dollin <chris.dol...@hp.comwrote:
jackl...@gmail.com wrote:
I have wrote a small function but got the error
function:
/*< squeeze */
void squeeze(char s1[],char s2[])
{
int de_s ;
int i,j,k;
for(i = 0;s2[i] !='\0';++i)
{
de_s = s2[i];

(Why not declare `de_s` here, rather than in the outer loop?)

(Why is it `int`, not `char`?)

(Why is it called `de_s`?)
for(j = k = 0;s1[j] != '\0';++j)
{
if(s1[j] != de_s)
{
s1[k++] = s1[j];

You're writing into the string `s1`.
}
}
s1[j] = '\0';
}
//printf("s1:%c",s1[0]);
}
/*>*///end-squeeze
I use squeeze("abcdefb","ab") to call the function.

You pass the string literal `"abcdefb"` as the value for the
argument `s1`.

You are not allowed to write into string literals.

You were lucky: BOOM.
Any suggestion?

Don't write into string literals, and don't use so many blank lines
and whitespace in your code.

void squeeze(char s1[],char s2[])
{
int i,j,k;
for(i = 0; s2[i] !='\0'; ++i)
{
char de_s = s2[i];
for(j = k = 0; s1[j] != '\0'; ++j)
{
if(s1[j] != de_s) s1[k++] = s1[j];
}
s1[j] = '\0';
}
//printf("s1:%c",s1[0]);

}

I won't comment on the algorithm itself.

--
"Possibly you're not recalling some of his previous plans." Zoe, /Firefly/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England
Thanks a lot!
I choose let
char s1[] = "abcdefb";
char s2[] = "ab";
squeeze(s1,s2);
It becomes right,it does the string literals problem.
May be I should use "temp" stand for "de_s"
and thank you for the advice
Nov 7 '08 #5

P: n/a
In article <gf**********@news-pa1.hpl.hp.comChris Dollin <ch**********@hp.comwrites:
....
void squeeze(char s1[],char s2[])
{
int i,j,k;
for(i = 0; s2[i] !='\0'; ++i)
{
char de_s = s2[i];
for(j = k = 0; s1[j] != '\0'; ++j)
{
if(s1[j] != de_s) s1[k++] = s1[j];
}
s1[j] = '\0';
I would make that s1[k] = '\0';
}
//printf("s1:%c",s1[0]);
}
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Nov 7 '08 #6

P: n/a
On Nov 7, 9:31 pm, "Dik T. Winter" <Dik.Win...@cwi.nlwrote:
In article <gf18ag$di...@news-pa1.hpl.hp.comChris Dollin <chris.dol...@hp.comwrites:

...
void squeeze(char s1[],char s2[])
{
int i,j,k;
for(i = 0; s2[i] !='\0'; ++i)
{
char de_s = s2[i];
for(j = k = 0; s1[j] != '\0'; ++j)
{
if(s1[j] != de_s) s1[k++] = s1[j];
}
s1[j] = '\0';

I would make that s1[k] = '\0';
}
//printf("s1:%c",s1[0]);
}
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland;http://www.cwi.nl/~dik/
Yeh,you are right ,I have change that expression ,it should be s1[k] =
'\0'
Nov 7 '08 #7

P: n/a
ja******@gmail.com wrote:
void squeeze(char s1[],char s2[])
[...modifying algorithm on s1...]
Thanks a lot!
I choose let
char s1[] = "abcdefb";
char s2[] = "ab";
squeeze(s1,s2);
Actually, you have the program running, but I'm afraid you haven't
understood the nature of the problem completely yet.

Firstly, the function

void squeeze(char s1[],char s2[]);

is actually a kind of a lie. The point is that there are no arrays passed to
that function. Instead, this is just a weird way of writing this:

void squeeze( char* s1, char* s2);

Now, since you only want to modify what s1 points to but not what s2 points
to, you can tell that to the compiler:

void squeeze( char* s1, char const* s2);
void squeeze( char* s1, const char* s2);

This allows the compiler to tell you when you modify a char that you
previously promised not to modify.

Note that both variants are equivalent. I prefer the first one, because
putting const to the right of what is const generally works, to the left
only in one case. You will see both variants in the wild though.

So, you can now safely invoke the function like

char s1[] = "abcdef";
squeeze( s1, "ab");

Note that here, other than in the function declaration, you can _not_ write

char* s1 = "abcdef";

This would compile though, unfortunately. In old C, there was no 'const', so
this had to compile. In current C, it is still accepted for backward
compatibility. Take the habit of not using that though.

Uli

Nov 8 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.