A strange string function error

J

jacklisp

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 !='\0';++i)
{
de_s = s2;

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 :)
 
P

Poter

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 !='\0';++i)
        {
                de_s = s2;

                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;"?
 
C

Chris Dollin

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 !='\0';++i)
{
de_s = s2;


(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 !='\0'; ++i)
{
char de_s = s2;
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.
 
I

Ian Collins

I have wrote a small function but got the error
function:
/*< squeeze */
void squeeze(char s1[],char s2[])
{
}
/*>*///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.
 
J

jacklisp

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 !='\0';++i)
{
de_s = s2;


(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 !='\0'; ++i)
{
char de_s = s2;
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
 
D

Dik T. Winter

> void squeeze(char s1[],char s2[])
> {
> int i,j,k;
> for(i = 0; s2 !='\0'; ++i)
> {
> char de_s = s2;
> 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]);
> }
 
J

jacklisp

...
void squeeze(char s1[],char s2[])
{
int i,j,k;
for(i = 0; s2 !='\0'; ++i)
{
char de_s = s2;
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]);
}


Yeh,you are right ,I have change that expression ,it should be s1[k] =
'\0'
 
U

Ulrich Eckhardt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top