今天 用指針的時候 遇到一個小問題
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int cmp(const void *a,const void *b)
{
return strcmp((char *)a,(char *)b);
}
int main()
{
int i;
char str[4][5];
char **temp;
strcpy(str[0],"dbc");strcpy(str[1],"cbc");strcpy(str[2],"bbc");strcpy(str[3],"abc");
//temp = (char **)str;
// i = 4;
for(i =0 ; i < 4; i ++)
{
printf("%d:%s\t",str[i],str[i]);
}
printf("\n");
for(temp = (char **)str,i =0 ; i < 4; i ++)
{
printf("%d:%s\t",temp,temp);
temp++;//這里++ 是會錯誤的 他沒有加 5個單位 只是加了四個
/*
1245032:dbc 1245037:cbc 1245042:bbc 1245047:abc
1245032:dbc 1245036:蘡bc 1245040: 1245044:c
*/
}
qsort(str,4,sizeof(char)*5,cmp);
printf("排序后\n");
for(i =0 ; i < 4; i ++)
{
printf("%d:%s\t",str[i],str[i]);
}
return 0;
}
但是 我將 改成這個 strcpy(str[0],"bc");strcpy(str[1],"bc");strcpy(str[2],"bc");strcpy(str[3],"bc");
也還是加 4 sizeof(char **) = 4;
無論什么指針都是 占四個字節 。
posted on 2010-07-03 23:41
付翔 閱讀(158)
評論(0) 編輯 收藏 引用 所屬分類:
linux 及 c相關