小编Bab*_*ack的帖子

将sockaddr_storage转换为sockaddr_in for inet_ntop

我正在尝试将sockaddr_storage转换为sockadd_in,以便我可以打印出数据报包的源IP地址,我似乎无法使得转换正确,

struct sockaddr_storage  peer_addr;


getnameinfo((struct sockaddr *) &peer_add
                peer_addrlen,
                hostbuff, sizeof(hostbuff),
                NULL, 0, NI_NAMEREQD);


inet_ntop(AF_INET, (((struct sockaddr_in *)peer_addr).sin_addr), ipbuff, INET_ADDRSTRLEN);
Run Code Online (Sandbox Code Playgroud)

当我尝试将结构转换为sockaddr_in时,我要么"无法转换为指针",要么当我删除dereferance时,我会'转换为非转换器类型'.

我尝试了很多组合,根本不明白我哪里错了.

c sockets

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

K&R 1.21,当插入打印阵列新线时

我正在通过K&R工作,我对我的程序的输出有点了解,当按下tab而不是在char数组行中插入一个tab时,if语句用于插入8个空白字符; 然而,当我按下回车键而不是获得所需的输出时,我的单词被换行符而不是空格或空格.问题1.21为好奇

    /*replaces tabs from input with equal number of spaces*/
#include <stdio.h>
#define MAXLINE 1000
int getliner(int maxlen, char line[]);
int main(){
    char line[MAXLINE];
    int len=0;
    while((len=getliner(MAXLINE, line))>0){
        printf("%s\n", line);
        len=0;
    }
    return 0;
}
/*getline: puts input in arr, returns lenght*/
int getliner(int max, char s[]){
    int i, j;
    char c;

    for(i=0;i<max-1 && (c=getchar())!=EOF && c!='\n' && c!='\t';++i)
        s[i]=c;
    if((c=='\t')){
        for(j=(i+8);i<j;i++)
            s[i]=' ';
        s[i]='\0';
    }
    if((c=='\n')){
        s[i]='\n';
        ++i;
    }
    s[i]='\0';
    return i;

}
Run Code Online (Sandbox Code Playgroud)

输出的一个例子

dom     dom   <-- this …
Run Code Online (Sandbox Code Playgroud)

c kernighan-and-ritchie

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

配置c ++标准库的clang-check

我试图运行Ale作为我的linter,然后使用clang-check来lint我的代码.

$ clang-check FeatureManager.h
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "FeatureManager.h"
No compilation database found in /home/babbleshack/ or any parent directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
/home/babbleshack/FeatureManager.h:6:10: fatal error: 'unordered_map' file not found
#include <unordered_map>
         ^~~~~~~~~~~~~~~
1 error generated.
Error while processing /home/babbleshack/FeatureManager.h.
Run Code Online (Sandbox Code Playgroud)

而使用clang ++进行编译只会返回一个警告.

$ clang++ -std=c++11 -Wall FeatureManager.cxx FeatureManager.h
clang-5.0: warning: treating 'c-header' input as 'c++-header' when in C++ mode, …
Run Code Online (Sandbox Code Playgroud)

c++11 clang++

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

标签 统计

c ×2

c++11 ×1

clang++ ×1

kernighan-and-ritchie ×1

sockets ×1