小编Nis*_*911的帖子

c使用va_list打印参数列表

我有一个简单的参数列表.我只是想把它打印到stdout,但我在打印"结束"之前得到了有线输出.有谁知道空行和不可读的字符来自哪里?

输出:

start
hello
hello2
hello3
hello 4

UH??AWAVAUATE1?S1?H??HH?E?
end



void printTest(const char* msg, ...) {

    va_list ap;
    int i;
    const char* curMsg=0;
    va_start(ap, msg);
    printf("start\n");

    for(curMsg= msg ;  curMsg!=0 ; curMsg = va_arg(ap,  const char*)){
        printf("%s\n", curMsg);
    }
    printf("end\n");
    va_end(ap);
}



int main(){

    printTest("hello", "hello2", "hello3", "hello 4");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c printf variadic-functions

3
推荐指数
1
解决办法
2864
查看次数

getaddrinfo错误:不支持ai_socktype

struct addrinfo *myAddrinfo, *curMyAddrinfo, hint;
memset(&hint, 0, sizeof(struct addrinfo));
hint.ai_family = AF_INET;
hint.ai_protocol = AI_PASSIVE;
hint.ai_socktype = SOCK_STREAM;

const int code = getaddrinfo(NULL, SERVER_PORT, &hint, &myAddrinfo);
if ((code) != 0) {
    printf("getaddrinfo error occours: %s ",
            gai_strerror(code));
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

这给出了错误:如果我注释掉hint.ai_protocol = AI_PASSIVE;它,它将通过:“不支持ai_socktype” ,但是我想知道为什么会发生?

谢谢你的时间

c sockets network-programming getaddrinfo

1
推荐指数
1
解决办法
6523
查看次数