在C字符数组指针 [英] Pointers with Character array in C

查看:148
本文介绍了在C字符数组指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将是什么以下程序的输出

What would be the output of the following program?

main( ) 
{ 
 char s[ ] = "Get organised! learn C!!" ; 
 printf ( "\n%s", &s[2] ) ; 
 printf ( "\n%s", s ) ; 
 printf ( "\n%s", &s ) ; 
 printf ( "\n%c", s[2] ) ; 
} 

我是在IM pression,这将打印出字符 T 第一个打印输出语句的地址。但不知何故,它打印出一切从字符 T

I was in the impression that it will print out the address of character t for the first print out statement. But somehow it is printing out everything starting from character t.

为什么会这样呢?我相信,我是混合指针了很多字符串。

Why is it so? I believe, I am mixing Pointers a lot with String.

这是我从上面的code获得输出。

Output that I am getting from the above code.

t organised! learn C!!
Get organised! learn C!!
Get organised! learn C!!
t

任何详细解释会有很大的帮助。

Any detailed explanations will be of great help.

推荐答案

当你说明是%S ,你告诉的printf 打印空终止字符串,它发现该地址。要打印地址,使用%P

When your specifier is %s, you tell printf to print the null-terminated string that it finds at that address. To print an address, use %p.

这篇关于在C字符数组指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆