#include <stdio.h> #include <stdlib.h> #include <string.h> char * reverse(char *string); int main(int argc, char *argv[]) { char array[10]; array[0] = 'a'; array[1] = 'b'; array[2] = 'c'; array[3] = 'd'; array[4] = 'e'; printf("1%s\n",array); char *p = reverse(array); printf("4%s\n",p); printf("5%s\n",array); } char * reverse(char *string) { int size = strlen(string); char reversed[size]; int i; int j = 0; for(i = size-1; i >= 0; i--) { reversed[j] = string[i]; j++; } printf("2%s\n",reversed); string = reversed; printf("3%s\n",string); return reversed; } This code basically just initializes an array of values and passes it into a method that reverses these values.
I am not sure if this is the best way to execute the task, since I am new to pointers and arrays in C.
But the real question is this:
Can anyone figure out why in this line
printf("4%s\n",p); if you remove the preceding '4', so it looks like so
printf("%s\n",p); the line won't print at all?