Reverse a message using pointers.
I am trying to reverse a message using pointers but I get only the last character of the message.
Here's the code :
Code:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int main (int argc, const char * argv[]) {
char myArray[100];
char ch;
int n = 0;
//int counter;
int *p;
//get user input
printf("Enter a mesage: ");
ch = getchar();
while (ch != '\n') {
myArray[n] = ch;
n++;
ch = getchar();
}//end while
//results
printf("Reversal is: ");
for (p = &myArray[n - 1]; p >= &myArray[0]; p--) {
printf("%c", *p);
}
}
The problem should be in the result section.
Am I doing something wrong there?