如何打印使用printf的一个字符串数组中的单个字符? [英] How to print a single character from an array of strings using printf?

查看:444
本文介绍了如何打印使用printf的一个字符串数组中的单个字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有

char *names[] = { "Tom", "Jerry" };

和我想要打印的E中的杰里用的printf 。我的第一反应是

and I want to print the "e" in "Jerry" using printf. My first instinct was

printf("%c\n", *names[5]);

但是当我申请我一直在学习三分球,我意识到这是总的垃圾code,因为5是指第五个不存在的指针名称 ,而不是E中的杰里。中包含的名指针将只引用的内存地址第一个在各自的字符串的字符。

but when I applied what I've been learning about pointers, I realized this is total junk code because the 5 refers to the nonexistent fifth pointer in names, not the "e" in "Jerry". The pointers contained in names will only ever refer to the memory addresses of the first characters in their respective strings.

所以看起来我真正需要做的是一个字节添加到名称[1] 为指向,并打印在杰里中的E。但我不知道如何做到这一点,还是它甚至允许在C

So it seems what I really need to do is to add one byte to names[1] to point to, and print the "e" in "Jerry". But I'm not sure how to do this, or whether it's even allowed in C.

什么是实现这一目标的最佳途径?谢谢你在前进。

What is the best way to accomplish this? Thank you in advance.

推荐答案

我想你要找的是什么的printf(%C \\ N,名字[1] [1]);

这篇关于如何打印使用printf的一个字符串数组中的单个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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