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 3 3864 mi************@ advalvas.be (=?ISO-8859-1?Q?Micha=EBl_B oland?=) 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
In <77************ **************@ posting.google. com> mi************@ advalvas.be (=?ISO-8859-1?Q?Micha=EBl_B oland?=) 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
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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Unixware/C++/AT&T componets
environment.
I have been able to compile all modules after making necessary changes
in LINUX/gcc/STL environment. We have two templates defined XList and
XMap.
|
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 closeing of
directories, I have fixed that though...who woulda thought that it would
be open, read, and closedir()?
Anyway, I am still working on the syntax, and I have been getting these
errors when I compile:
UEngine.cpp: In member...
|
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 type of "const char *".
Right? But why does the compiler I am using allow s to be modified, instead
of generating compile error?
|
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
|
by: d3x0xr |
last post by:
---- Section 1 ----
------
x.c
int main( void )
{
char **a;
char const *const *b;
b = a; // line(9)
| |
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, and you'll
be left with just noisy compiler warnings and confusion.
if you start a project with all char *, and char ** and even char ***,
if you begin at the low level weeding out references of 'passing const
char * to char * ( such as...
|
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 data that I get in argv ?
Q2. what is encoding of string constants defined in programs (for
example L"--count") ?
|
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 some of the tools provided on the command-line. If you hate the command line tools, get over it since you’re bound to be using them at some point in your career. All commands in Linux ARE case sensitive so capital letters are different from lowercase...
|
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 that's causing
the problem.
Here's how I wrote the program originally:
#include <stdio.h>
#include <string.h>
|
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...
|
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,...
| |
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...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |