I want help regarding a function which you call from main that reverse text. However, the program works, well "more or less" but it crashes. Here is how code looks
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void reverse(char * array, int numberOfChars) {
int begin = 0;
int end = 0;
char temp;
end = strlen(&array) - 1;
printf("%s", &array);
while (begin < end) {
temp = array[begin];
array[begin] = array[end];
array[end] = temp;
begin++;
end--;
}
}
int main() {
reverse('supm', 4);
return(0);
getchar();
}
The string gets reversed to mpus, but then crashes, apparently it seems also like the arrays can only take in 4 characters, if i change it to 5 and the integer value to 5, it won't work at all. Any help would be appreciated.
strlen(&array)
and printf("%s", &array);
are wrong. They will pass where the pointer is instead of where the string is. use strlen(array)
and printf("%s", array);
.
reverse('supm', 4);
is also wrong because the first argument of this call is not a string but an implementation-defined integer.
Be careful that just changing it to reverse("supm", 4);
won't work because modifying string literals isn't allowed.
Also, the last getchar();
won't be executed because it is after return 0;
.
Try this:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void reverse(char * array, int numberOfChars) {
int begin = 0;
int end = 0;
char temp;
end = strlen(array) - 1;
printf("%s", array);
while (begin < end) {
temp = array[begin];
array[begin] = array[end];
array[end] = temp;
begin++;
end--;
}
}
int main() {
char supm[] = "supm";
reverse(supm, 4);
puts(supm); /* added this to see if this code is working well */
return(0);
}
Maybe you should use the parameter numberOfChars
instead of strlen(array)
because you pass the parameter, but it is not used at all.
A few things immediately pop out at me:
char*
, where you are passing a char**
. Instead of strlen(&array)
, use strlen(array)
. printf
. Instead of printf("%s", &array)
, use printf("%s", array)
. 'supm'
should be "supm"
. You also cannot modify a string literal, so you'll need to set a variable containing your string and then pass the variable instead. return(0)
before calling getchar
. That means getchar
will never be called. numberOfChars
is unused. Either remove that parameter or use it instead of strlen
. In your function reverse
-
end = strlen(&array) - 1;
printf("%s", &array);
You pass char **
to strlen
which expects a const char *
and try to print a char **
with %s
. Jusr pass array
to both these functions. And in main
-
reverse('supm', 4);
If you even do "spum"
which would be string literal , thus constant (modifying it would cause problem).
Do this instead -
char s[]="spum";
reverse(s, 4);
Compiler must have issued warnings for these statements and if not then enable compiler warnings.
array
is already a pointer-- &array
gives you a pointer to the pointer. so change the strlen call to this:
strlen(array);
you need to define the string before you pass to the function-- otherwise the string only exists inside your function, you won't be able to access it outside.
char mystr[] = "supm"; reverse(mystr, 4); printf("%s\\n", mystr); return 0;
Try this code I have fixed all your code problems.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void reverse(char * array, int numberOfChars) {
int begin = 0;
int end = 0;
char temp;
end = strlen(array) - 1;
printf("%s\n", array);
while (begin < end) {
temp = array[begin];
array[begin] = array[end];
array[end] = temp;
begin++;
end--;
}
}
int main() {
char str[] ="sump";
reverse(str, 4);
puts(str);
return(0);
}
What you can learn from problems of this code ?
void reverse(char * array, int numberOfChars) /* definition */
reverse('supm', 4); /*calling function */
Compare this both two in definition first argument is to pass character array char *array
But in calling you are passing 'sump'
.Now onwards please remember for more than character you can use ""
so here you should use "sump"
end = strlen(&array) - 1;
Refer strlen
Its argument is constant character pointer . But here you are passing &array
.I guess you think &array
is address of array but that is wrong for array array
represent address and array[i]
represent value
getchar()
This function is for reading input not for printing strings. refer getchar
For printing reversed output you should use puts at last not getchar
In your main
function, the line reverse('supm', 4);
has a couple of things wrong with it. First of all, it is a character constant, not a string, so you will end up passing an invalid pointer value to your reverse
function. You probably meant to use "supm"
which is a string and does match the char *
expected by your reverse
function. However, "supm"
is a string literal constant, so although you can point to it, you are not allowed to modify it. So you should do something like this:
char rev[] = "supm";
reverse(rev, 4);
Here, rev
is an array of 5 char
, initialized as {'s', 'u', 'p', 'm', '\\0'}
.
You are not currently using the numberOfChars
parameter of reverse
, so you could remove it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.