この場合、arrayname [i]が*(arrayname + i)と等しくないのはなぜですか?奇妙なこと:この場合、arrayname [i]が*(arrayname + i)と等しくないのはなぜですか?
#include <iostream>
using namespace std;
struct fish
{
char kind[10] = "abcd";
int weight;
float length;
};
int main()
{
int numoffish;
cout << "How many fishes?\n";
cin >> numoffish;
fish *pfish = new fish[numoffish];
cout << pfish[0].kind << endl; //the output is "abcd"
/*if the above code is changed to
"cout << (*pfish.kind);"
then compile error happens */
/*and if the above code is changed to
"cout << (*pfish->kind);"
then the output is only an "a" instead of "abcd"*/
delete [] pfish;
return 0;
}
はあなたのコードでは、あなたの質問に一致していません。あなたは正しく 'a [i]'が '*(a + i)'と同じであることを正しく述べます。あなたのコードには 'pfish [0]'が含まれています。その規則によれば '*(pfish + 0)'と同じです。ここまでは順調ですね。しかし、コードサンプルに '(* pfish.kind)'が含まれています。これは '*(pfish + 0)'とまったく異なります。 –
@ M.Mはい、ありがとう。私はちょうど(* pfish.kind)が(* pfish).kind –