「.」と「->」の違い
構造体を関数に渡す際に、値渡しの時は渡した先の関数で「.」を使っても構わないが、参照渡しの時は渡した先の関数で「.」を使うとエラーになる。「->」であればエラーにならない。
環境は以下の通りなのだけど、あんまり関係ないような気もする。
C言語自体の仕様なのかなぁ?
$ uname -a Linux akira-laptop 2.6.28-11-generic #42-Ubuntu SMP Fri Apr 17 01:57:59 UTC 2009 i686 GNU/Linux $ gcc --version gcc (Ubuntu 4.3.3-5ubuntu4) 4.3.3 Copyright (C) 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//sample code #include <stdio.h> struct data{ char *ch; }; int func1(struct data *d1) { //ここをd1.chにすると //error: request for member ‘ch’ in something not a structure or union //と言われる。 printf("%s", d1->ch); return 0; } int func2(struct data d2) { printf("%s", d2.ch); return 0; } int main() { struct data d; d.ch = "hello"; func1(&d); func2(d); return 0; }