473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How can we get rid of the last element of a string in C

10
Hi,

How can we get rid of the last character of a string in c???

Thanks
Toktam
May 12 '07 #1
7 3468
JosAH
11,448 Expert 8TB
Hi,

How can we get rid of the last character of a string in c???

Thanks
Toktam
Change that last character to a '\0'.

kind regards,

Jos
May 12 '07 #2
toktam
10
Change that last character to a '\0'.

kind regards,

Jos
Thanks for that Jos,

But actually I have a string of type char* ,I changed it to an array of characters to change the last character...I need to return char* type so I returned the name of that array but I got segmentation fault...

For example:
char str[20];
str[19]='\o'
returne str;

but It gave an error...
May 12 '07 #3
JosAH
11,448 Expert 8TB
Thanks for that Jos,

But actually I have a string of type char* ,I changed it to an array of characters to change the last character...I need to return char* type so I returned the name of that array but I got segmentation fault...

For example:
char str[20];
str[19]='\o'
returne str;

but It gave an error...
That array 'str' is a local variable. Once you return from your function your
entire array is lost for the posterity (as is every local variable).

kind regards,

Jos
May 12 '07 #4
AdrianH
1,251 Expert 1GB
Thanks for that Jos,

But actually I have a string of type char* ,I changed it to an array of characters to change the last character...I need to return char* type so I returned the name of that array but I got segmentation fault...

For example:
char str[20];
str[19]='\o'
returne str;

but It gave an error...
Well you can't do it that way. Declaring the array on the stack like that will cause undefined results as that array will be available to be written over when you return from the function.

If you are passed a char* and you manipulate that one passed, it will work (unless you pass a literal c-string, i.e. "this is a literal c-string" -- note the double quotes, in which case you could get a seg fault).

If you don't want to manipulate that one, either
  1. allocate a new one and specify that the caller of the function must deallocate it
  2. pass two arrays, one to be read from and the other to be written to -or-
  3. return a C++ string object (NOT A REFERENCE or POINTER but an OBJECT), and let the compiler deal with the deletion of the object

Hope this helps.


Adrian
May 12 '07 #5
Hi, i think that you are using the variable str as a local variable. and you are trying to pass it to another function. if you want to stick to your choice then you can declare the str variable as a global variable, not a local. Try it and wish you best of luck.
May 12 '07 #6
AdrianH
1,251 Expert 1GB
Hi, i think that you are using the variable str as a local variable. and you are trying to pass it to another function. if you want to stick to your choice then you can declare the str variable as a global variable, not a local. Try it and wish you best of luck.
You are invoking the dark forbidden arts of the global variable. BEWARE!!!!! Woooooo!!! (supposed to sound spooky ;))

Global variables are not recommended to be used except under close watch and exceptional circumstances. They make the code messy and unmaintainable. If you need a global variable, you really should have a relook at your design.


Adrian
May 12 '07 #7
toktam
10
You are invoking the dark forbidden arts of the global variable. BEWARE!!!!! Woooooo!!! (supposed to sound spooky ;))

Global variables are not recommended to be used except under close watch and exceptional circumstances. They make the code messy and unmaintainable. If you need a global variable, you really should have a relook at your design.


Adrian
Thanks alot guys for answering my question...
It helped me alot...
May 13 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

32
by: James Curran | last post by:
I'd like to make the following proposal for a new feature for the C# language. I have no connection with the C# team at Microsoft. I'm posting it here to gather input to refine it, in an "open...
2
by: Sharkie | last post by:
I'm relatively new to XSLT, having strong background in bunch of other programming languages (Perl, Java, etc.). I'm writing an XSLT stylesheet, converting XML into HTML. Everything works just...
3
by: Miro | last post by:
First off...thanks in advance for getting me this far. Sorry for all these class posts but im having a heck of a time here trying to get something to work, and have finally got it to work (...
2
by: Kevin Burton | last post by:
I don't think I understand the last() function. I have a document that looks like: <Root> <Header>Some text</Header> <Message> <MessageID>1</MessageID> . . . . </Message>
3
by: CCLeasing | last post by:
Hi i'm trying to check if the last line in a file is equal to a string i'm comparing it with. I have the general idea of what I should be doing. Could someone take a look at the following and...
8
by: sherifffruitfly | last post by:
Hi, I've been searching as best I can for this - coming up with little. I have a file that is full of lines fitting this pattern: (?<year>\d{4}),(?<amount>\d{6,7}) I'm likely to get a...
5
by: junky_fellow | last post by:
Hi, I discussed about this earlier as well but I never got any satisfactory answer. So, I am initiating this again. Page 84, WG14/N869 "If both the pointer operand and the result point to...
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
16
by: Juha Nieminen | last post by:
I'm actually not sure about this one: Does the standard guarantee that if there's at least one element in the data container, then "--container.end()" will work and give an iterator to the last...
23
by: Florian Lindner | last post by:
Hello, can I determine somehow if the iteration on a list of values is the last iteration? Example: for i in : if last_iteration: print i*i else:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.