2

The following is the exercise 10.4 of the book Programming in C, by Stephen Kochan. It says that I should create a function, that derives a part from an input string and returns that part back to main() (as a string, not a pointer) and displays it. My code is below.

#include <stdio.h>

char subString (const char source[], int start, int count, char result[count + 1] ){ //result will be number of characters (count) + 1 (because of null)
int i, j, end = start + count;

// the part excluded must start from i = start and "count" number of characters must be derived and then put on result
for( i = start, j = 0; i < end; ++i, ++j)
    result[j] = source[i];

result[j] = '\0';

return result[count + 1];
}

int main (void){
char result[20] = {0};

const char text1[] = "character";

result[20] = subString( text1, 4, 3, result );
printf("From \"%s\" this part is being excluded-> \"%s\"\n", text1, result);

return 0;
}

And the output is

From "character" this part is being excluded-> "act"

Process returned 0 (0x0)   execution time : 0.332 s
Press any key to continue.

Notice that the code above runs perfectly well - no warnings.

What i can't understand is when i replace the two lines below

result[20] = subString( text1, 4, 3, result );
printf("From \"%s\" this part is being excluded-> \"%s\"\n", text1, result);

with this line

printf("From \"%s\" this part is being excluded-> \"%s\"\n", text1, subString( text1, 4, 3, result ) );

i get the output:

From "character" this part is being excluded-> "(null)"

Process returned 0 (0x0)   execution time : 0.332 s
Press any key to continue.

Why is that? How can i make it work using that one line instead? Also, I'm a little confused with functions returning strings/arrays. They tend to lead me on errors, so if someone could provide me a couple of suggestions, that i should always keep in mind when working with them, that would help me very much. Thank you in advance.

5
  • 1
    there is no valid element called result[20]. Hint: 0 based index. Commented Apr 15, 2015 at 11:18
  • 1
    You might need to sanity-check the parameters and ensure that start+count doesn't go out of bounds of the source string. And the loop should be de-obfuscated into this: for(i = 0; i<count; i++) result[i] = source[i+start]; Commented Apr 15, 2015 at 11:37
  • @Sourav Ghosh I initialized the string with 20 places in memory and put a null in it, because someone told me here a while ago, that i should always filling null terminators in them when i initialize them (regardless of size) since that's good programming practice. Commented Apr 15, 2015 at 12:19
  • @Lundin yes you're right, that would make things easier. Commented Apr 15, 2015 at 12:22
  • @RestlessC0bra Please see my answer. I'm taking about the 21st element, which does not exist. :-) Commented Apr 15, 2015 at 12:22

2 Answers 2

2

Point 1 :

In First case, you are using the result value which got modified through being passed as argument to subString(). You're not using the return value of subString () fucntion.

OTOH, in second approach, you're trying to use the rerturn value of the subString () function, that too using wrong format specifier. You can learn more about the correct format specifiers in the man page of printf()

Point 2

Array index in C starts from 0. So, there is no valid element called result[20]. Thus,

result[20] = subString( text1, 4, 3, result );

cause off-by-one error which in turn invokes undefined behaviour.

Sign up to request clarification or add additional context in comments.

4 Comments

..ah i see (about point 2). Yes i know these. Yes i didn't mean to put it on 21st element. I thought that this was the right way to type the array and store inside the string i want. I'm still looking into this. I haven't understood completely. And yes, point 1 very true, didn't realize that. But still i want to figure out how to return string from function (and not pointer). Still looking..
@RestlessC0bra yeah Please look carefully. Double check the data types, return values and format specifiers always. :-)
@RestlessC0bra what do you mean by eturn string from function (and not pointer).? what is the represenation of a string? You locate and use a string by the base address which is a pointer itself. Then?
Yes, yes but i'm talking about using string terminology (strictly a string) and not use pointers. I know that in essence a string is a pointer. Since this exercise was in the book before pointers were presented, it's asking me to do it without using pointers.
1
printf("From \"%s\" this part is being excluded-> \"%s\"\n", text1, subString( text1, 4, 3, result ) );

Make a note that subString() returns a character not a char* and you are using %s to print a character which will lead to undefined behavior.

Your array is being modified in the function subString() and in the first case you print the array result by returning result[count + 1] and also there is a undefined behavior with

result[20] = subString( text1, 4, 3, result );

Array out of bound access

You need to modify your code like

char *subString (const char source[], int start, int count, char result[]){ //result will be number of characters (count) + 1 (because of null)
int i, j, end = start + count;

// the part excluded must start from i = start and "count" number of characters must be derived and then put on result
for( i = start, j = 0; i < end; ++i, ++j)
    result[j] = source[i];

result[j] = '\0';

return result;
}

int main()
{
  // Keep your array here
  char *p = subString( text1, 4, 3, result );
  printf("%s\n",p);
}

or

void subString (const char source[], int start, int count, char result[]){ //result will be number of characters (count) + 1 (because of null)
    int i, j, end = start + count;

    // the part excluded must start from i = start and "count" number of characters must be derived and then put on result
    for( i = start, j = 0; i < end; ++i, ++j)
        result[j] = source[i];

    result[j] = '\0';

    }

In main()

printf("%s\n",result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.