473,770 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A strange string function error

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).Bu t it doesn't work and I got
the message form GDB:
Starting program: /home/jack/myPro/myC/ListTest/CprogrammingTes t/
Char01/main

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

I use squeeze("abcdef b","ab") to call the function.
Any suggestion?
Thanks :)
Nov 7 '08 #1
7 1823
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).Bu t it doesn't work and I got
the message form GDB:
Starting program: /home/jack/myPro/myC/ListTest/CprogrammingTes t/
Char01/main

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

I use squeeze("abcdef b","ab") to call the function.
Any suggestion?
Thanks :)
why "int de_s ;" but not "char des_s;"?
Nov 7 '08 #2
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("abcdef b","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
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).Bu t it doesn't work and I got

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

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

--
Ian Collins
Nov 7 '08 #4
On Nov 7, 7:21 pm, Chris Dollin <chris.dol...@h p.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("abcdef b","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
In article <gf**********@n ews-pa1.hpl.hp.comC hris Dollin <ch**********@h p.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
On Nov 7, 9:31 pm, "Dik T. Winter" <Dik.Win...@cwi .nlwrote:
In article <gf18ag$di...@n ews-pa1.hpl.hp.comC hris Dollin <chris.dol...@h p.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
1871
by: Neil Zanella | last post by:
Hello, I wonder whether anyone has ever come across the following g++ compiler error message. I don't recall ever seeing it before. I solved my problem but I am still not sure about what this message is all about. Any ideas? error: invalid initialization of non-const reference of
2
8927
by: Olaf | last post by:
I have a frameset page witch contains the myFuc() function. The function is accessed from a page in one of the frames in the frameset. An example is shown below. <input onclick="javaScript:alert('document.forms(0)='+document.forms(0)); parent.myFunc(document.forms(0));" type="button" value="Open" name="Button" ID="Button"> The strange part is that the debug alert says that the document.forms(0) is an object så all seem to be well. But...
1
2265
by: John | last post by:
Hi First of all apologies for posting so much code but this is rather involved and I am totally stumped. Basically I have a main form (Staff Batch SMS/E-Mail) which calls a function (SendSMS) in a module with in turn calls a form (frmInet) which contains an ms internet control (Inet1). Problem is that once the function Send SMS returns and the code tries to close the main form a 'Runtime error 2486: You can't carry out this action at...
1
2354
by: sofakingfree | last post by:
This error is driving me nuts. The code below will run perfectly when it has a breakpoint. But as soon as I remove the breakpoint and run it normally outside of debug mode I get a strange error message that says: "Microsoft Office PowerPoint has encountered a problem and needs to close. We are sorry for the inconvience. If you were in the middle of something, the information you were working on might be lost."
6
2110
by: Rick | last post by:
Hi, Sorry, I couldn't find a better title for this post. Anyway, I got a piece of C code which only works well if I put a fprintf in it. Here's the code : unsigned int8 Port_getPortNum( char *port ) { fprintf(DB9, "port = %s\r\n", port ); // Don't know why but I need this fprintf?! if ( port == 'A' )
3
1695
by: Jim in Arizona | last post by:
I'm going insane! I don't know if it's just that the .net 2.0 framework is buggy or if it really is my code. This is pretty hard to explain since I can't even begin to nail down why this is happening. I have two text boxes. One is for the ID number, which when postback occurs, inserts into the related table as the foreign key. The other text box is for notes that go along side the foreign key of the the related table. Basically, the...
2
2647
by: Piedro | last post by:
Can someone reproduce the following error? I'm using the module at the bottom of my post to owner draw a menu items, I call the module from a form like this: Private Sub mnuOpen_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles mnuOpen.DrawItem Dim Ic As New Icon(Application.StartupPath & "\101_72.ico") DrawItems(e, mnuOpen, Ic) End Sub
1
1183
by: Maileen | last post by:
Hi, I finished my application butonce again i have some strange behavior with XML/text functions... for example, here below is a function which worked perfectly till now and now generate an error : "An unhandled exception of type 'System.NullReferenceException' occured in Unknown Module. Addition information : object reference not set to an instance of object"
2
4114
by: peter | last post by:
Hi, I have very strange situation but first description ;) I have: 1) project in VB.NET, in this f.e. 1 function: Public Function Login(ByVal UserName As String, ByVal UserPassword As String, Optional ByVal ConnectionParamList As String = Nothing) As String
4
2426
by: Hole | last post by:
Hi There! I'm trying to use Zope and the product OpenFlow. I got the following error while I was using the built-in function getattr() to retrieve an OpenFlow object: attribute name must be string
0
9595
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9432
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8891
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.