Joh*_*ohn 14 pointers objective-c
我明白有一个星号*是指针,有两个**是什么意思?
我从文档中偶然发现了这个:
- (NSAppleEventDescriptor *)executeAndReturnError:(NSDictionary **)errorInfo
Run Code Online (Sandbox Code Playgroud)
pax*_*blo 33
它是一个指向指针的指针,就像在C中一样(尽管它采用奇怪的方括号语法,Objective-C基于):
char c;
char *pc = &c;
char **ppc = &pc;
char ***pppc = &ppc;
Run Code Online (Sandbox Code Playgroud)
等等,无限期(或直到你用完可变空间).
它通常用于将指针传递给必须能够更改指针本身的函数(例如为可变大小的对象重新分配内存).
=====
根据您的示例请求显示如何使用它,这里是我为另一篇文章编写的一些代码,用于说明它.它是一个appendStr()
管理自己分配的功能(你仍然需要释放最终版本).最初将string(char *
)设置为NULL,函数本身将根据需要分配空间.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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;
}
Run Code Online (Sandbox Code Playgroud)
/* 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);
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
代码输出以下内容.当前分配的内存用尽扩展字符串的空间时(注释处),您可以看到指针如何变化:
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.]
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您传入,**str
因为您需要能够更改*str
值.
=====
或者以下,对不在数组中的字符串执行展开的冒泡排序(哦,耻辱!).它通过直接交换字符串的地址来实现.
#include <stdio.h>
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;
}
Run Code Online (Sandbox Code Playgroud)
产生:
Unsorted: [77] [55] [99] [88] [66]
Sorted: [55] [66] [77] [88] [99]
Run Code Online (Sandbox Code Playgroud)
不要介意排序的实现,只需注意变量的传递方式,char **
以便它们可以轻松交换.任何真正的排序都可能是作用于真正的数据数组而不是单个变量,但这不是示例的重点.