如何使用指针字符定位指针数组?

huangapple go评论77阅读模式
英文:

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 &lt;stdio.h&gt;
int main(){
	char name[5];
char *p = &amp;name;
int i;
printf(&quot;Enter  the array&quot;);
for(i=0;i&lt;5;i++){
	scanf(&quot;%c&quot;,p);
	p++;
}
p--;
for(i=0;i&lt;5;i++){
	printf(&quot;%c&quot;,*(p));
	p--;
}
	

}

答案1

得分: 1

表达式&amp;name是指向数组的指针。它的类型将是char (*)[5]

您想要一个指向数组第一个元素的指针,即&amp;name[0]

请记住(或学习),普通的name本身会衰变成指向其第一个元素的指针。

因此,p的初始化应该是

char *p = name;

不过,您的代码中还有一些其他问题。例如,scanf格式%c不会跳过前导空格,就像由<key>Enter</key>键添加的换行符一样。如果您逐个字符输入,然后输入<key>Enter</key>键,那么您读取的第二个字符将是换行符。您需要添加一个前导空格,以告诉scanf跳过空白字符:&quot; %c&quot;

还要注意,您没有创建一个字符串。您有一个字符数组,但您从未添加字符串的空终止符&#39;\0&#39;字符。因此,虽然您对数组的使用是正确的,但您永远无法将其传递给任何期望空终止字符串的函数。

英文:

The expression &amp;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 &amp;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: &quot; %c&quot;.

Also note that you don't create a string. You have an array of characters, but you never add the string null-terminator &#39;\0&#39; 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];

因此,指针表达式&amp;name具有指针类型char (* )[5]。此表达式用于初始化类型为char *的指针。

char *p = &amp;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 &amp;name has the pointer type char ( * )[5]. This expression is used to initialize a pointer of the type char *

 char *p = &amp;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( &quot;Enter  the array: &quot; );

for ( ; p != name + N; ++p )
{
    scanf( &quot; %c&quot;, p );
}

while ( p != name ) printf( &quot;%c&quot;, *--p) ;
putchar( &#39;\n&#39; );

Pay attention to the leading space in the format string in this call of scanf

scanf( &quot; %c&quot;, 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 &#39;\n&#39;.

huangapple
  • 本文由 发表于 2023年7月10日 20:03:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定