星期一, 1月 11, 2010

C/C++筆記



*p++ or *(p++) Value of expression is *p before increment;increment p later

(*p)++ Value of expression is *p before increment;increment *p later

*++p or *(++p) Increment p first;value of expression is *p after increment

++*p or ++(*p) Increment *p first;value of expression is *p after increment



void ccc(int* a)
{
a[0]=3;
}
main()
{
int w[10]={1,2};
printf("%d ",w[0]);
ccc(w);
printf("%d \n",w[0]);
}

output:
1 3 //array can be modified on function



char *p = "abc";
p = 'd'; //wrong, because p point to the string, not char
*p = 'd'; //wrong, because *p not a char
p = (char []){'d'}; //correct

char c;
char *p="abc";
p = &c;
*p = 'b';

char *s="abcd";
s[0]='q'; //wrong
printf("%d",*(s+1)=='b'); //correct, output is 1
// can use compared operator, but can not use assigned operator



char *ps; || char *ps="abc";
strcpy(ps,"xyz"); //wrong, because pointer can't be changed direct

char s[]="abc";
char *ps = s;
strcpy(ps,"xyz"); //correct

沒有留言:

張貼留言