473,395 Members | 1,986 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,395 software developers and data experts.

susbtring function

Hello again,

I´ve made a small substring() function that shoudl give back the a
susbtring from a string, given its start and end positions.

Is there any chance of the code below give an output containing data
from another memory area that is completely unrelated to the input
string? This is happening and the start/end parameters are acceptable
(within bounds) for the input string...any idea?
char *substring(char *string, int start, int end){
printf("Input %s:", string);

char *result = (char *)malloc((end - start + 1)*sizeof(char));
if(result == NULL){
return(NULL);
}

string = string + start;
while(start < end){
*result = *string;
result++;
string++;
start++;
}
printf("Output %s:", result);
return(result);
}

Sep 14 '07 #1
5 1685
Ian Collins <ia******@hotmail.comwrites:
HS*********@gmail.com wrote:
[...]
> printf("Input %s:", string);

You forgot the '\n':

printf("Input %s:", string);
So did you. Just as a matter of esthetics, putting the ':' at the end
seems odd. I'd write this as:

printf("Input: \"%s\"\n", string);

[...]
> return(NULL);

return isn't a function

return NULL;
To expand on that a bit: the syntax of a return statement is
return ;
or
return <expression;

The parentheses are not necessary, but they are permitted. If you use

return(NULL);

then the parentheses are part of the expression, not part of the
syntax of the return statement itself.

I prefer *not* to use extraneous parentheses; a return statement isn't
a function call, so it shouldn't look like one.

But using parentheses isn't wrong. Even the examples in K&R1 use
parentheses on return statements (though K&R2 changed this).

[...]

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 15 '07 #2
HS*********@gmail.com wrote:
>
I´ve made a small substring() function that shoudl give back the a
susbtring from a string, given its start and end positions.

Is there any chance of the code below give an output containing data
from another memory area that is completely unrelated to the input
string? This is happening and the start/end parameters are acceptable
(within bounds) for the input string...any idea?

char *substring(char *string, int start, int end){
start and end are indices into *string, so should be type size_t
printf("Input %s:", string);

char *result = (char *)malloc((end - start + 1)*sizeof(char));
You can't put this after code. Declare result before the printf
statement. Delete the cast. Eliminate "*sizeof(char)", because
that is 1 by definition.
if(result == NULL){
return(NULL);
}

string = string + start;
Where have you checked that start is a valid index for this
string? Maybe "if (start < strlen(string)) ... " would do.
while(start < end){
Where have you checked that end is a valid index for this string?
Where have you checked that end is larger than start? See above.
*result = *string;
Maybe this expression should include start?
result++;
Maybe this action should disappear?
string++;
start++;
Indenting code controlled by a conditional is considered cool.
}
printf("Output %s:", result);
return(result);
This isn't the value malloc returned. You have a problem when you
free, or you have a memory leak.
}
Enjoy.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com

Sep 15 '07 #3
Charlie Gordon wrote:
>
All these corrections are fine and dandy...
I hope the were more than that.
to complete the answer for the
OP, there are 2 reasons you can get data in the output unrelated to in
input:
- you forgot the final '\0' after the copy loop. Insert *result = '\0';
after the }.
- you don't check that start and end fall within the boundaries of the
string. If not, you are attempting to read from beyond the end of the
string or before its beginning or indeed from no particular location... This
invokes undefined behaviour and does not necessarily cause your program to
crash.
There were at least three, as I pointed out, he was returning one past
the end of the result.

--
Ian Collins.
Sep 15 '07 #4
HS*********@gmail.com writes:
I´ve made a small substring() function that shoudl give back the a
susbtring from a string, given its start and end positions.

Is there any chance of the code below give an output containing data
from another memory area that is completely unrelated to the input
string?
As others described it is. :)
This is happening and the start/end parameters are acceptable
(within bounds) for the input string...any idea?

char *substring(char *string, int start, int end){
printf("Input %s:", string);

char *result = (char *)malloc((end - start + 1)*sizeof(char));
if(result == NULL){
return(NULL);
}

string = string + start;
while(start < end){
*result = *string;
result++;
string++;
start++;
}
printf("Output %s:", result);
return(result);
}
You may use strncpy() but remember to put 0 byte at the end of the
string:

#v+
char *substring(const char *string, size_t start, size_t end) {
return strcnpy(calloc(end-start+1), string+start, end-start);
}
#v-

Exercise: make the code readable, check if memory allocation succeed,
check if start is valid string's index and [start, end] is
non-empty set.

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Sep 15 '07 #5
Charlie Gordon wrote:
Michal Nazarewicz wrote:
<snip>
>#v+
char *substring(const char *string, size_t start, size_t end) {
return strcnpy(calloc(end-start+1), string+start, end-start);
}
#v-

Assuming strcncpy was a typo for the infamous strncpy,
There is a typo in the correction to the previous typo :)

<snip>

Sep 15 '07 #6

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
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...
0
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
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...
0
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...
0
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,...

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.