今天 用指針的時(shí)候 遇到一個(gè)小問(wèn)題
#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++;//這里++ 是會(huì)錯(cuò)誤的 他沒(méi)有加 5個(gè)單位 只是加了四個(gè)
/*
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;
}
但是 我將 改成這個(gè) strcpy(str[0],"bc");strcpy(str[1],"bc");strcpy(str[2],"bc");strcpy(str[3],"bc");
也還是加 4 sizeof(char **) = 4;
無(wú)論什么指針都是 占四個(gè)字節(jié) 。
posted on 2010-07-03 23:41
付翔 閱讀(159)
評(píng)論(0) 編輯 收藏 引用 所屬分類(lèi):
linux 及 c相關(guān)