2012年11月1日 星期四

struct 什麼時候要用點(.) 什麼時候要用箭頭 (->)

struct 什麼時候要用點(.)  什麼時候要用箭頭 (->)
當你new一個struct時候,就用 點(.)
當這個struct不是你new出來的,就用箭頭 (->)訪問他
目前暫時是這樣理解的

I mean ,
In struct data type, when we should use dot / period (.) to access its data
                                when we should use arrow (->) to access its data

if we " new " a struct , we should use dot / period (.) to access its data
here is an example as below:


#include   <stdio.h>
#include   <stdlib.h>
struct   point
{
      int   x,y;
};

int   main(int   argc,   char   *argv[])
{
    struct     point   one;
    one.x=1;
    one.y=2;
    printf( "%d         %d\n ",one.x,one.y);
    struct     point   *two;
    two=&one;
    printf( "%d         %d\n ",two-> x,two-> y);
   
    system( "PAUSE ");
    return   0;
}


************************
1       2(one.x和one.y)
1       2(two.x和two.y)

沒有留言:

張貼留言