4 12 C:\Users\Nishu\OneDrive\Desktop\test\Untitled1.cpp [Error] cannot convert ‘char ()[5]’ to ‘char‘ in initialization
i tried to reverse and array using pointer but it shows an array, i tried it with and without ‘&’, without it, it only scan 2 values and then reverse it.
#include <stdio.h>
int main(){
char name[5];
char *p = &name;
int i;
printf("Enter the array");
for(i=0;i<5;i++){
scanf("%c",p);
p++;
}
p--;
for(i=0;i<5;i++){
printf("%c",*(p));
p--;
}
}
>Solution :
The expression &name is a pointer to the array. It will have the type char (*)[5].
You want a pointer to the first element of the array, which is &name[0].
And please remember (or learn) that plain name will by itself decay to a pointer to its first element.
So the initialization of p should be
char *p = name;
There are a few other problems in your code though. For example the scanf format %c will not skip leading space, like the newline added by the Enter. If you give one character at a time as input, followed by the Enter key then the second character you read will be a newline. You need to add a leading space to tell scanf to skip white-space: " %c".
Also note that you don’t create a string. You have an array of characters, but you never add the string null-terminator '\0' character. So while your use of the array is fine, you can never pass it to any function expecting a null-terminated string.