PTR [英] ptr
问题描述
大家好。
我对这个程序有疑问
#include< stdio.h>
main()
{
char str1 [] =" hello";
char * p = hello;
p =" bye" ;
}
根据这个程序,p是指向字符串hello的指针。当我们指向
指向再见时。只有3个字母应该擦除。但它是再见。
为什么?
Hi everyone
I have a doubt in this program
#include<stdio.h>
main()
{
char str1[]="hello";
char *p=hello;
p="bye";
}
According to this program p is pointer to the string hello.When we made
to point to bye.Only 3 letters should erase.But it is giving bye .
Why?
推荐答案
嗨!
考虑你的程序这个小修改:
#include< stdio.h>
main()
{
char str1 [] =" hello";
char * p =" hello";
printf(" ;%u =%s",p,p);
p =" bye";
printf("%u =%s",p,p) ;
}
要观察的输出:
4202496 =你好
4202510 = bye
这意味着两个字符串hello和再见出现在2
不同的内存位置,通过更改指针
''p'指向你的地址,观察到的输出是合理的。
如果我的解释错误,请纠正我。
Hi!
Consider this small modificationt o your program:
#include<stdio.h>
main()
{
char str1[]="hello";
char *p="hello";
printf("%u = %s",p,p);
p="bye";
printf("%u = %s",p,p);
}
Output to be observed:
4202496 = hello
4202510 = bye
which means that the two strings "hello" and "bye" are present at 2
different memory locations and by changing the address to which pointer
''p'' points to you, the observed output is justified.
Please correct me if my interpretation is wrong.
vim写道:
vim wrote:
大家好
我对这个程序有疑问
我也有疑问,因为它根本没有编译:
gcc.exe -c main.c -pedantic -W -Wall -Wextra -ansi
main.c中包含的文件:1:
main.c:3:警告:返回类型默认为int'
main.c:在函数main中:
main.c:5:错误:`hello''未声明(在此函数中首次使用)
main.c:5:错误:(每个未声明的标识符仅报告一次
main.c:5:错误:对于它出现的每个函数。)
main.c:4:警告:未使用的变量`str1''
make。 exe:*** [m ain.o]错误1
执行终止
#include< stdio.h>
main()
{
char str1 [ ] =" hello";
char * p = hello;
p =" bye";
}
根据这个程序,p是指向字符串hello的指针。当我们指示再见。只有3个字母应该擦除。但它是再见。
为什么?
Hi everyone
I have a doubt in this program
I have a doubt as well, since it doesn''t compile at all:
gcc.exe -c main.c -pedantic -W -Wall -Wextra -ansi
In file included from main.c:1:
main.c:3: warning: return type defaults to `int''
main.c: In function `main'':
main.c:5: error: `hello'' undeclared (first use in this function)
main.c:5: error: (Each undeclared identifier is reported only once
main.c:5: error: for each function it appears in.)
main.c:4: warning: unused variable `str1''
make.exe: *** [main.o] Error 1
Execution terminated
#include<stdio.h>
main()
{
char str1[]="hello";
char *p=hello;
p="bye";
}
According to this program p is pointer to the string hello.When we made
to point to bye.Only 3 letters should erase.But it is giving bye .
Why?
[如果你修复它,并添加一个`printf()`因此它输出
的东西。]
因为你指定给`p`的不是字符串bye但指向
常量字符串文字bye的指针。在'str1`的情况下,你所写的
只是数组初始化的简写:
char str1 [] = {''h' ',''','''',''',''o'',''\ 0''};
你可能意味着:
char * p =" hello";
再次向`p`指定一个指向常量的指针string literal
" hello" ;.
常量字符串文字保存在内存中(你不知道,
并且不小心),你不能修改它们,即:
* p =''Z'';
>
是非法的。
[Provided you fix it, and add a `printf()` so it does output
something.]
Because what you assign to `p` is not a string "bye" but a pointer to a
constant string literal "bye". In case of `str1`, what you''ve written
is just shorthand for array initialisation:
char str1[] = {''h'',''e'',''l'',''l'',''o'',''\0''};
In the second line you probably meant:
char *p = "hello";
which again assigns to `p` a pointer to a constant string literal
"hello".
Constant string literals are held somewhere in memory (you don''t know,
and shoudln''t care), and you''re not allowed to modify them, i.e.:
*p = ''Z'';
is illegal.
HandledException写道:
HandledException wrote:
嗨!
考虑一下你的程序的这个小修改:
#include< stdio.h>
main()
int main (无效)
{
char str1 [] =" hello"; char * p =" hello";
printf("%u =%s",p,p);
输出指针的唯一正确方法是使用%p说明符。
p =" bye";
printf("%u =%s",p,p);
如果你没有以''\ n''终止ouptut,你可能根本看不到它
(你可以使用` fflush()`以及。)
此外,在C99中不需要,但仍然很好:
返回0;
(注意,C99不允许`main()`的隐式类型,所以你的代码不是
C99,而`return`实际上是,需要。)
}
要观察的输出:
4202496 =你好
4202510 = bye
这意味着两个字符串你好和再见存在于2个不同的存储位置,通过改变指针
''p'指向你的地址,观察到的输出是合理的。
请纠正我,如果我的解释是错误的。
Hi!
Consider this small modificationt o your program:
#include<stdio.h>
main()
int main(void)
{
char str1[]="hello";
char *p="hello";
printf("%u = %s",p,p);
The only correct way to output pointers is using "%p" specifier.
p="bye";
printf("%u = %s",p,p);
If you don''t terminate ouptut with a ''\n'', you may not see it at all
(you can use `fflush()` as well).
Also, not required in C99, but nice still:
return 0;
(NB, C99 wouldn''t allow implicit type for `main()`, so your code is not
C99, and the `return` is, in fact, required.)
}
Output to be observed:
4202496 = hello
4202510 = bye
which means that the two strings "hello" and "bye" are present at 2
different memory locations and by changing the address to which pointer
''p'' points to you, the observed output is justified.
Please correct me if my interpretation is wrong.
这不是解释,而是实验。你解释了*什么*
发生了,而不是*如何*或*为什么*。您还在我上面指出的corected
代码中出错。
引用上下文。阅读< http://cfaj.freeshell.org/google/>。
It''s not interpretation, it''s experimentation. You explained *what*
happens, not *how* or *why*. You also made errors in your "corected"
code which I pointed out above.
Quote context. Read <http://cfaj.freeshell.org/google/>.
这篇关于PTR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!