473,503 Members | 9,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linux const char type

I have a fonction to trim a string :

char *trimstr(char *pbuf1)
{
char *pbuf2 , *pbuf3;

pbuf2 = pbuf1 ;
pbuf3 = pbuf1 ;

while(*pbuf1 != '\0') { if(*pbuf1 != ' ') { pbuf2 = pbuf1; pbuf2++; }
pbuf1++; }
while(*pbuf2 != '\0') { *pbuf2 = '\0'; pbuf2++; }
return(pbuf3) ;
}

main(int argc, char *argv[], char *env[])
{
trimstr(" tata ");

return 0;
}

It's ok with Unix Digital.
The result with Linux is Segmentation fault.
I think the problem is " tata " is a const *char and C Linux don't
allow to change a constant.
Is it possible in the function trimstr to know if the argument is a
*char or a const *char?
Or anything else solution?

Regards.

Michaël
Nov 13 '05 #1
3 3851
mi************@advalvas.be (=?ISO-8859-1?Q?Micha=EBl_Boland?=) wrote:
I have a fonction to trim a string :

char *trimstr(char *pbuf1)
{
char *pbuf2 , *pbuf3;

pbuf2 = pbuf1 ;
pbuf3 = pbuf1 ;

while(*pbuf1 != '\0') { if(*pbuf1 != ' ') { pbuf2 = pbuf1; pbuf2++; }
pbuf1++; }
while(*pbuf2 != '\0') { *pbuf2 = '\0'; pbuf2++; }
return(pbuf3) ;
}
That's a funny way to write a trimming function, but let that be...
main(int argc, char *argv[], char *env[])
This is not a valid declaration of main(). Main is either

int main(void)

or

int main(int argc, char **argv)

or anything equivalent; it cannot take a third argument in ISO C. In
C99, implicit int is out as well, but you've probably got a C89
compiler, so that's OK.
{
trimstr(" tata ");

return 0;
}

It's ok with Unix Digital.
Only by accident.
The result with Linux is Segmentation fault.
I think the problem is " tata " is a const *char
Not quite; it's a non-modifiable array of char. For historical and
convenience reasons, it isn't actually const. And in this case, the
convenience just happens to be inconvenient :-(
and C Linux don't allow to change a constant.
Correct. And rightly so.
Is it possible in the function trimstr to know if the argument is a
*char or a const *char?


No. That is, yes; if it _had_ been a const char *, you would have had a
compile-time diagnostic. But since string literals aren't actually
const-qualified, there is no way to tell. Just don't pass string
literals to functions which modify them; it invokes undefined behaviour,
and the effects may range from accidentally working, through seeming to
work but doing nothing, causing a segfault, and in theory mailing lurid
proposals to Elio Di Rupo. So just don't do that.
Oh, btw: it's a char *, not a * char.

Richard
Nov 13 '05 #2
In <77**************************@posting.google.com > mi************@advalvas.be (=?ISO-8859-1?Q?Micha=EBl_Boland?=) writes:
I have a fonction to trim a string :

char *trimstr(char *pbuf1)
{
char *pbuf2 , *pbuf3;

pbuf2 = pbuf1 ;
pbuf3 = pbuf1 ;

while(*pbuf1 != '\0') { if(*pbuf1 != ' ') { pbuf2 = pbuf1; pbuf2++; }
pbuf1++; }
while(*pbuf2 != '\0') { *pbuf2 = '\0'; pbuf2++; }
return(pbuf3) ;
}

main(int argc, char *argv[], char *env[])
{
trimstr(" tata ");

return 0;
}

It's ok with Unix Digital.
The result with Linux is Segmentation fault.
I think the problem is " tata " is a const *char and C Linux don't
allow to change a constant.
Is it possible in the function trimstr to know if the argument is a
*char or a const *char?
Or anything else solution?


Never pass the address of a string literal to *any* function that doesn't
promise (one way or another) not to use it for writing purposes.

Therefore, the solution to your problem is not inside trimstr() but
outside it.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #3
On 25 Jul 2003 02:08:52 -0700, mi************@advalvas.be (Michaël
Boland) wrote in comp.lang.c:
I have a fonction to trim a string :

char *trimstr(char *pbuf1)
{
char *pbuf2 , *pbuf3;

pbuf2 = pbuf1 ;
pbuf3 = pbuf1 ;

while(*pbuf1 != '\0') { if(*pbuf1 != ' ') { pbuf2 = pbuf1; pbuf2++; } ^^^^^^^^^^^^^
ITYM *pbuf2 = *pbuf1;
pbuf1++; }
while(*pbuf2 != '\0') { *pbuf2 = '\0'; pbuf2++; }
return(pbuf3) ;
}

main(int argc, char *argv[], char *env[])
{
trimstr(" tata ");

return 0;
}

It's ok with Unix Digital.
The result with Linux is Segmentation fault.
I think the problem is " tata " is a const *char and C Linux don't
allow to change a constant.
Is it possible in the function trimstr to know if the argument is a
*char or a const *char?
Or anything else solution?

Regards.

Michaël


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13105
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on...
4
3231
by: Jon | last post by:
For the past month I have been porting a DirectX windows game to libSDL on linux, for right now I am working on the basic syntax of the game engine, I was having problems with opening, reading, and...
7
4344
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
5
5657
by: cranium.2003 | last post by:
hi, Here is my code #include <iostream.h> int main() { cout <<"HI"; return 0; } and using following command to compile a C++ program g++ ex1.cpp -o ex1
10
2758
by: d3x0xr | last post by:
---- Section 1 ---- ------ x.c int main( void ) { char **a; char const *const *b; b = a; // line(9)
0
1859
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
4
9746
by: interec | last post by:
Hi Folks, I am writing a c++ program on redhat linux using main(int argc, wchar_t *argv). $LANG on console is set to "en_US.UTF-8". g++ compiler version is 3.4.6. Q1. what is the encoding of...
2
20810
jwwicks
by: jwwicks | last post by:
C/C++ Programs and Debugging in Linux This tutorial will give you a basic idea how to debug a program in Linux using GDB. As you are aware Visual Studio doesn’t run on Linux so you have to use...
27
3102
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
I have a fully-portable C program (or at least I think I do). It works fine on Windows, but malfunctions on Linux. I suspect that there's something I don't know about the standard input stream...
0
7193
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,...
0
7264
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,...
0
7316
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6975
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
4992
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...
0
3160
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3148
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1495
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 ...
0
371
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.