C return line function gets strange output

I am trying to understand pointers and make a return line function.
the code:

#include <stdio.h> #include <stdlib.h> #include <string.h> char *reverseString(char string[]){ int i; int len = strlen(string); char reversedString[len]; for (i = 0; i < len; i++){ reversedString[i] = string[(len - 1) - i]; } printf("%s", reversedString); //print it out return reversedString; //return pointer to first element of reversed string } int main (){ char string[6] = "kitten"; int i; char *p = reverseString(string); return (0); } 

My goal is to change the line "kitten" and print the return line. I expect nettik to exit, but I get nettik. Why am I getting these weird characters?

+4
source share
3 answers

Stop! First of all, you are making a classic mistake that newcomers often make when they learn about pointers. When you write:

 char *reverseString(char string[]) { ... char reversedString[len]; return reversedString; } 

You return a pointer to where it was, not is: the memory is actually freed when you leave the function, so there is no guarantee that the memory you think contains your return line is no longer reused, Thus, your program will catastrophically catastrophic.

However, you print the inverted string before returning it, so that is not a problem yet. Then the wrong thing is that your line does not have a zero end.

A C string needs room to store the final byte '\0' : char string[6] = "kitten"; too small to hold a null-terminated string { 'k', 'i', 't', 't', 'e', 'n', '\0' } . Similarly, when you call printf("%s", reversedString) , you didn’t finish the line correctly with '\0' , so printf continues to search for the end of the line and prints all the unwanted files to the memory where reversedString is allocated.

You must try:

  • returning your void function,
  • allocation of 7 bytes for your source string,
  • highlighting strlen(string) + 1 (which will also be 7) bytes for your new line and
  • writing '\0' to the end of your new line after scrolling through the old line in reverse order.
+11
source

There are at least two major errors in the code. Maybe more.

First, calling strlen invokes undefined behavior because you are not passing it a null-terminated string. The reason for this is because your char array is not large enough for a null terminator character:

 char string[6] = "kitten"; 

You need

 char string[7] = "kitten"; 

or

 char string[] = "kitten"; 

Secondly, you are returning a pointer to a local array, namely reversedString . Failure to link will also result in undefined behavior. You can solve this problem by flipping the line in place or by passing a pointer to a buffer of the same length as the input. Remember that the return line must also end with zero.

+9
source

The function is incorrect.

Firts of all returns a pointer to a local array that will be destroyed after exiting the function

 //,,, char reversedString[len]; //... return reversedString; //return pointer to first element of reversed string 

Secondly, the return line must end with zero. However, you declare an array that has no room for null termination.

Also this array is mostly misdefined

 char string[6] = "kitten"; 

since it does not include zero completion.

And finally, this is a poor function design.

If you want to copy the source string to the destination string, then both character arrays must be declared as function parameters. In addition, the source array must be declared as a constant array.

The function might look like this

 char *reverseCopyString( char s1[], const char s2[] ) { size_t n = strlen( s2 ); for ( size_t i = 0; i < n; i++ ) s1[i] = s2[n - i - 1]; s1[n] = '\0'; return s1; } 

Or you can define a function in such a way as to invert the original string. for instance

 char *reverseString( char s[] ) { size_t n = strlen( s ); for ( size_t i = 0; i < n / 2; i++ ) { char c = s[i]; s[i] = s[n - i - 1]; s[n - i - 1] = c; } return s; } 

Note that string literals are immutable in C and cannot be changed. Any attempt to change the string literal results in undefined program behavior.

Here is a demo program

 #include <stdio.h> #include <string.h> char *reverseCopyString( char s1[], const char s2[] ) { size_t n = strlen( s2 ); for ( size_t i = 0; i < n; i++ ) s1[i] = s2[n - i - 1]; s1[n] = '\0'; return s1; } char *reverseString( char s[] ) { size_t n = strlen( s ); for ( size_t i = 0; i < n / 2; i++ ) { char c = s[i]; s[i] = s[n - i - 1]; s[n - i - 1] = c; } return s; } int main( void ) { char *s1 = "Hello gilianzz"; char s2[16]; puts( s1 ); puts( reverseCopyString( s2, s1 ) ); puts( reverseString( s2 ) ); } 

Program exit

 Hello gilianzz zznailig olleH Hello gilianzz 
+1
source

All Articles