C 的入門者請進,否則不要花時間click (轉)

worldblog發表於2007-12-10
C 的入門者請進,否則不要花時間click (轉)[@more@]

//只需要把下面程式碼paste到new project, run, 即看到效果
//The goal of this program is to show:
// the relationshof Pointer and Address in C languange.

//created by Feb 4th, 2002 
//modified by Feb 4th,2004

#include
#include
#include

 //if put  3, result=3^3+3=12;
 //if  4, result=4^4+4=20...
double squarePlus(int a,double *b);

int main(void)
{
  double x=3,y=3,result=0;
  printf("n 0.....y Address = %pn",&y); // print y address

 //scanf(y);
  result=squarePlus(3,&y); 
 
  printf("n SquarePlus of %f = %2.0f n",x,result);

 return 0;
}
/*

 0.....y Address = 0012FF70
 1.....pAddress(b) = 0012FF70
 2.....double value(*b) = 3.000000
...processing: (*b) *= *b;

 SquarePlus of 3.000000 = 12
Press any key to continue

*/

//**********************************************************//
double squarePlus(int a,double *b)
{
  //Print the address of pointer:
  printf(" 1.....pAddress(b) = %p n",b);

  //Print the value after calcuation:
  printf(" 2.....double value(*b) = %fn",*b);

  //Save before you have to change.
  double k = *b; 
  //b=&k; //[YES] if b= sth's address;
  //  *b= sth's value;


  // b is always the Result:
  *b=a;
  (*b) *= *b;  //multipile itself
  printf("...processing: (*b) *= *b;n");
 
  *b=*b+k;  //[YES] value itself +1;
  //b=b+b;  //[NO]  just make "b" to another address;

  return *b;
  //return (*b)*(*b)+(*b);  //[YES] works also
  //return b;  // [NO] cannot convert from 'double *' to 'double'
}

//?FN_id=15&FC__id=115073&FC_id=115073



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-991103/,如需轉載,請註明出處,否則將追究法律責任。

相關文章