英文:
how to use pointer character to locate pointer array?
问题
[错误] 无法将 'char ()[5]' 转换为 'char' 进行初始化
尝试使用指针反转数组,但它显示一个数组,我尝试过使用'&'和不使用它,不使用它时,只扫描2个值,然后将其反转。
#include <stdio.h>
int main(){
char name[5];
char *p = &name;
int i;
printf("输入数组");
for(i=0; i<5; i++){
scanf("%c", p);
p++;
}
p--;
for(i=0; i<5; i++){
printf("%c", *(p));
p--;
}
}
英文:
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--;
}
}
答案1
得分: 1
表达式&name
是指向数组的指针。它的类型将是char (*)[5]
。
您想要一个指向数组第一个元素的指针,即&name[0]
。
请记住(或学习),普通的name
本身会衰变成指向其第一个元素的指针。
因此,p
的初始化应该是
char *p = name;
不过,您的代码中还有一些其他问题。例如,scanf
格式%c
将不会跳过前导空格,就像由<key>Enter</key>键添加的换行符一样。如果您逐个字符输入,然后输入<key>Enter</key>键,那么您读取的第二个字符将是换行符。您需要添加一个前导空格,以告诉scanf
跳过空白字符:" %c"
。
还要注意,您没有创建一个字符串。您有一个字符数组,但您从未添加字符串的空终止符'\0'
字符。因此,虽然您对数组的使用是正确的,但您永远无法将其传递给任何期望空终止字符串的函数。
英文:
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 <key>Enter</key>. If you give one character at a time as input, followed by the <key>Enter</key> 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.
答案2
得分: 0
你声明了一个包含5个元素的字符数组。
char name[5];
因此,指针表达式&name
具有指针类型char (* )[5]
。此表达式用于初始化类型为char *
的指针。
char *p = &name;
但是,这两种指针类型之间没有隐式转换。
相反,你需要写成这样:
char *p = name;
在这种情况下,数组标识符name
会隐式转换为指向其第一个元素的char *
类型的指针。
至于使用指针以相反顺序输出数组的任务,这种情况下声明变量i
是多余的。你可以这样写:
enum { N = 5 };
char name[N];
char *p = name;
printf("输入数组:");
for (; p != name + N; ++p)
{
scanf(" %c", p);
}
while (p != name) printf("%c", *--p);
putchar('\n');
请注意在scanf
调用中格式字符串的开头空格:
scanf(" %c", p);
这允许跳过空白字符。如果用户在输入每个字符后按Enter键,例如:
B
o
b
b
y
那么数组将包含序列Bobby
。如果在格式字符串中没有开头的空格,数组将包含换行字符'\n'
。
英文:
You declared a character array with 5 elements.
char name[5];
So the pointer expression &name
has the pointer type char ( * )[5]
. This expression is used to initialize a pointer of the type char *
char *p = &name;
However there is no implicit conversion between the pointer types.
Instead you need to write
char *p = name;
In this case the array designator name
is implicitly converted to a pointer to its first element of the type char *
.
As for the task to output an array in the reverse order using a pointer then the declaration of the variable i
in this case is redundant. You could write
enum { N = 5 };
char name[N];
char *p = name;
printf( "Enter the array: " );
for ( ; p != name + N; ++p )
{
scanf( " %c", p );
}
while ( p != name ) printf( "%c", *--p) ;
putchar( '\n' );
Pay attention to the leading space in the format string in this call of scanf
scanf( " %c", p );
^^^
It allows to skip white space characters. That is if the user will enter a namd pressing the Enter ley after each entered character as for example
B
o
b
b
y
then the array will contain the sequence Bobby
. Without the leading space in the format string the array will contain also new line characters '\n'
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论