![](/img/trans.png)
[英]I cannot get Eclipse MinGW and Sqlite to Work Together for a C program
[英]Cant get C program and strsep() and getenv() to all work together
我以前曾做過這項工作,但是我正在使用指針。 getenv()一直崩潰,所以我使用sprintf()復制了結果。 現在我想用:代替,只打印第一次出現。 請幫忙!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main(void) {
char buf[999];
const char *token;
// HTTP_PROXY == 8.8.8.8:8888, end result should print 8.8.8.8
sprintf(buf, "%s", getenv("HTTP_PROXY"));
*token = strsep(&buf, ":");
printf("New result: %s\n", token);
}
由於strsep
一個指向指針的指針,因此必須傳遞一個指向指針的指針,而不是一個指向數組的指針。 這不是一回事。 創建一個指針,並將其分配給buf
。 將指向該新指針的指針傳遞給strsep
以解決第一個問題。
第二個問題是,由於strsep
返回一個指針,因此您需要將其分配給token
,而不是*token
:
char buf[999];
const char *token;
// HTTP_PROXY == 8.8.8.8:8888, end result should print 8.8.8.8
sprintf(buf, "%s", getenv("HTTP_PROXY"));
char *ptr = buf; // Since ptr, is a pointer...
token = strsep(&ptr, ":"); // ...you can pass a pointer to pointer
printf("New result: %s\n", token);
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char const *http_proxy = getenv("HTTP_PROXY");
if (http_proxy == NULL) {
fprintf(stderr, "HTTP_PROXY not set: default 8.8.8.8:8888");
http_proxy = "8.8.8.8:8888";
}
char *cpy = strdup(http_proxy);
char *token = strtok(cpy, ":");
if (token == NULL) {
fprintf(stderr, "wrong format");
return 1;
}
do {
printf("Token: %s\n", token);
} while ((token = strtok(NULL, ":")) != NULL);
free(cpy);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.