c语言中 两个星号 **,Objective-C中有两个星号**是什么意思?

它是一个指针,就像在C(尽管它的奇怪的方括号语法,Objective-C是基于)的指针:

char c;

char *pc = &c;

char **ppc = &pc;

char ***pppc = &ppc;

等等,无穷无尽(或直到你用完可变空间).

它通常用于将指针传递给必须能够更改指针本身的函数(例如为可变大小的对象重新分配内存).

=====

在您请求一个示例显示如何使用它之后,以下是为另一篇文章编写的代码.它是一个appendStr()函数,它管理自己的分配(您仍然必须释放最终版本).最初,您将字符串(char *)设置为NULL,函数本身将根据需要分配空间.

#include

#include

#include

void appendToStr (int *sz, char **str, char *app) {

char *newstr;

int reqsz;

/* If no string yet, create it with a bit of space. */

if (*str == NULL) {

*sz = strlen (app) + 10;

if ((*str = malloc (*sz)) == NULL) {

*sz = 0;

return;

}

strcpy (*str, app);

return;

}

/* If not enough room in string, expand it. We could use realloc

but I've kept it as malloc/cpy/free to ensure the address

changes (for the program output). */

reqsz = strlen (*str) + strlen (app) + 1;

if (reqsz > *sz) {

*sz = reqsz + 10;

if ((newstr = malloc (*sz)) == NULL) {

free (*str);

*str = NULL;

*sz = 0;

return;

}

strcpy (newstr, *str);

free (*str);

*str = newstr;

}

/* Append the desired string to the (now) long-enough buffer. */

strcat (*str, app);

}

static void dump(int sz, char *x) {

if (x == NULL)

printf ("%8p [%2d] %3d [%s]\n", x, sz, 0, "");

else

printf ("%8p [%2d] %3d [%s]\n", x, sz, strlen (x), x);

}

static char *arr[] = {"Hello.", " My", " name", " is", " Pax",

" and"," I", " am", " old."};

int main (void) {

int i;

char *x = NULL;

int sz = 0;

printf (" Pointer Size Len Value\n");

printf (" ------- ---- --- -----\n");

dump (sz, x);

for (i = 0; i < sizeof (arr) / sizeof (arr[0]); i++) {

appendToStr (&sz, &x, arr[i]);

dump (sz, x);

}

}

代码输出以下内容.当扩展字符串(在注释中)当前分配的内存空间不足时,您可以看到指针如何更改:

Pointer Size Len Value

------- ---- --- -----

# NULL pointer here since we've not yet put anything in.

0x0 [ 0] 0 []

# The first time we put in something, we allocate space (+10 chars).

0x6701b8 [16] 6 [Hello.]

0x6701b8 [16] 9 [Hello. My]

0x6701b8 [16] 14 [Hello. My name]

# Adding " is" takes length to 17 so we need more space.

0x6701d0 [28] 17 [Hello. My name is]

0x6701d0 [28] 21 [Hello. My name is Pax]

0x6701d0 [28] 25 [Hello. My name is Pax and]

0x6701d0 [28] 27 [Hello. My name is Pax and I]

# Ditto for adding " am".

0x6701f0 [41] 30 [Hello. My name is Pax and I am]

0x6701f0 [41] 35 [Hello. My name is Pax and I am old.]

在这种情况下,您传入** str,因为您需要能够更改* str值.

=====

或者下面这个对于不在数组中的字符串进行展开的气泡排序(哦,耻辱!)它通过直接交换字符串的地址来实现.

#include

static void sort (char **s1, char **s2, char **s3, char **s4, char **s5) {

char *t;

if (strcmp (*s1, *s2) > 0) { t = *s1; *s1 = *s2; *s2 = t; }

if (strcmp (*s2, *s3) > 0) { t = *s2; *s2 = *s3; *s3 = t; }

if (strcmp (*s3, *s4) > 0) { t = *s3; *s3 = *s4; *s4 = t; }

if (strcmp (*s4, *s5) > 0) { t = *s4; *s4 = *s5; *s5 = t; }

if (strcmp (*s1, *s2) > 0) { t = *s1; *s1 = *s2; *s2 = t; }

if (strcmp (*s2, *s3) > 0) { t = *s2; *s2 = *s3; *s3 = t; }

if (strcmp (*s3, *s4) > 0) { t = *s3; *s3 = *s4; *s4 = t; }

if (strcmp (*s1, *s2) > 0) { t = *s1; *s1 = *s2; *s2 = t; }

if (strcmp (*s2, *s3) > 0) { t = *s2; *s2 = *s3; *s3 = t; }

if (strcmp (*s1, *s2) > 0) { t = *s1; *s1 = *s2; *s2 = t; }

}

int main (int argCount, char *argVar[]) {

char *a = "77";

char *b = "55";

char *c = "99";

char *d = "88";

char *e = "66";

printf ("Unsorted: [%s] [%s] [%s] [%s] [%s]\n", a, b, c, d, e);

sort (&a,&b,&c,&d,&e);

printf (" Sorted: [%s] [%s] [%s] [%s] [%s]\n", a, b, c, d, e);

return 0;

}

产生:

Unsorted: [77] [55] [99] [88] [66]

Sorted: [55] [66] [77] [88] [99]

没关系排序的实现,只要注意变量被传递为char **,以便它们可以很容易的交换.任何真正的排序都可能是对真正的数据数组而不是单个的变量进行操作,但这并不是这个例子.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值