#include <stdio.h>
int add(int , int );
int prnch(int , int );
int mul(int , int);
int main(){
int (*fptr[3])(int, int) = {add, mul, prnch};
int x = 10;
int y = 20;
int z=0;
int i;
for(i=0;i<3;i++){
z += (*fptr[i])(x, y);
}
printf("z: %d\n", z);
return 0;
}
int add(int a, int b){
return printf("sum: %d\n", a+b);
}
int mul(int a, int b){
return printf("mul: %d\n", a*b);
}
int prnch(int m, int n){
while(n--){
putchar('-');
}
putchar('\n');
while(m--){
putchar('-');
}
putchar('\n');
return m - n;
}
the output is:
sum: 30
mul: 200
--------------------
----------
z: 17
why Z is equal to 17?
2011年2月22日星期二
about the pointer
after reviewing Fardad's lecture, i have a question about the address.
#include <stdio.h>
int main(){
int a[3][5]={
{10,20,30,40,50},
{100,200,300,400,500},
{1000,2000,3000,4000,5000}
};
printf("%u\n", a);
printf("%u\n", a+1);
printf("%u\n", a[0]);
printf("%u\n", a[0]+1);
printf("%d, %d\n", *(*(a+2) + 3), a[2][3]);
return 0;
}
in my opinion, the first one "a" is the same as the third one "a[0]". so they have the same address.
but is it the second one "a+1" = "a[0]+1"????
the output is:
2030780
2030800
2030780
2030784
4000, 4000
i don't know why???
#include <stdio.h>
int main(){
int a[3][5]={
{10,20,30,40,50},
{100,200,300,400,500},
{1000,2000,3000,4000,5000}
};
printf("%u\n", a);
printf("%u\n", a+1);
printf("%u\n", a[0]);
printf("%u\n", a[0]+1);
printf("%d, %d\n", *(*(a+2) + 3), a[2][3]);
return 0;
}
in my opinion, the first one "a" is the same as the third one "a[0]". so they have the same address.
but is it the second one "a+1" = "a[0]+1"????
the output is:
2030780
2030800
2030780
2030784
4000, 4000
i don't know why???
2011年2月1日星期二
int2str
const char* Int2Str(int num) {
static char string[50];
int count=1;
int temp=num;
int i=0;
while(num=num/10){
count++;
}
while(count)
{
string[count-1]= 48+temp%10;
temp=temp/10;
count--;
}
return string;
}
static char string[50];
int count=1;
int temp=num;
int i=0;
while(num=num/10){
count++;
}
while(count)
{
string[count-1]= 48+temp%10;
temp=temp/10;
count--;
}
return string;
}
订阅:
博文 (Atom)