我使用strtok()编写了一个简单的url解析器.这是代码
#include <stdio.h>
#include <stdlib.h>
typedef struct {
char *protocol;
char *host;
int port;
char *path;
} aUrl;
void parse_url(char *url, aUrl *ret) {
printf("Parsing %s\n", url);
char *tmp = (char *)_strdup(url);
//char *protocol, *host, *port, *path;
int len = 0;
// protocol agora eh por exemplo http: ou https:
ret->protocol = (char *) strtok(tmp, "/");
len = strlen(ret->protocol) + 2;
ret->host = (char *) strtok(NULL, "/");
len += strlen(ret->host);
//printf("char at %d => %c", len, url[len]);
ret->path = (char …Run Code Online (Sandbox Code Playgroud) 我正在尝试在函数内部分配和初始化一个数组,但我似乎无法在返回后获取值.
这是我最近几乎在努力的尝试
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int func(int **thing);
int main() {
int *thing;
func(&thing);
printf("%d %d", thing[0], thing[1]);
}
int func(int **thing) {
*thing = calloc(2, sizeof(int));
*thing[0] = 1;
*thing[1] = 2;
printf("func - %d %d \n", *thing[0], *thing[1]);
}
Run Code Online (Sandbox Code Playgroud)
但是在函数外部打印的值是1和0.有很多关于指针的文档,但我没有发现这个特定的案例.关于我做错的任何提示?