最后一个printf(“%s”,* title [5]);没有输出,为什么? [英] The last printf("%s", *title[5]); isn't coming in output, why?

查看:89
本文介绍了最后一个printf(“%s”,* title [5]);没有输出,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
int main()
{
char title[]={"tale of 2 cities","wuthering heights","don quixote","odyssey","moby dick","hamlet","gulliver travel"};
charbestbooks[3];
charenglishbooks[4];
bestbooks[0]=&title[0];
bestbooks[1]=&title[3];
bestbooks[2]=&title[5];
englishbooks[0]=&title[0];
englishbooks[1]=&title[1];
englishbooks[2]=&title[5];
englishbooks[3]=&title[6];
printf("%s\n",englishbooks[2]);
englishbooks[2]="changed";
printf("%s\n",*englishbooks[2]);
printf("%s",*title[5]);
return 0;
}





我的尝试:



最后一个printf也应该显示输出:changed。



What I have tried:

the last printf should also display output:"changed".

推荐答案

:叹气:

请看看我对你的最后一个答案,并试着想一想你在做什么。

你展示的代码将无法编译:

:sigh:
Please, look at my last answer to you and try to think about what you are doing.
The code you show won't compile:
char title[]={"tale of 2 cities","wuthering heights","don quixote","odyssey","moby dick","hamlet","gulliver travel"};

不对:这是一个字符数组 - 或一个字符串 - 你试图将它预设为任何字符串数组。它必须是:

Isn't right: thats an array of chars - or a string - and you are trying to preset it to any array of strings. It needs to be:

char* title[]={"tale of 2 cities","wuthering heights","don quixote","odyssey","moby dick","hamlet","gulliver travel"};



然后你的printf仍然赢了;工作:


But then your printf still won;t work:

printf("%s",*title[5]);

因为title是一个指向字符的指针数组(如果你愿意,还是一个字符串数组),所以title [5]是一个指针to char或string,* title [5]是一个char - 但是你告诉printf它是一个指针,所以最好你得到一个分段错误。

试试

because title is an array of pointers to characters (or an array of strings if you prefer) and so title[5] is a pointer to a char, or a string, and *title[5] is a char - but you tell printf it's a pointer, so at best you'd get a segmentation fault.
Try

printf("%s",title[5]);

请仔细看看你在做什么,并尝试将代码翻译成英文 - 你传递的内容变得非常明显。

and please, look carefully at what you are doing and try translating the code into English in your head - it becomes pretty obvious what you are passing.


这篇关于最后一个printf(“%s”,* title [5]);没有输出,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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