c - Mac 上连接字符集时出现总线错误 10

标签 c arrays string function int

你能告诉我这段代码有什么问题吗:

#include <stdio.h>
#if __STDC_VERSION == 199901L
#include <stdint.h>
#include <string.h>
#else
/* change to fit the compiler */
typedef unsigned char uint8_t;
typedef unsigned long uint32_t;
#endif
#define IPV4
#if defined(IPV4)
#define NBYTES 4
#elif defined(IPV6)
#error IPV6 not supported
#else
#error No valid IP version protocol defined
#endif

char* concat(char *s1, char *s2)
{
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
    //in real code you would check for errors in malloc here
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

int main()
{
    uint32_t ipAddress = 167840383;
    uint8_t  octet[NBYTES];
    int x;
    char buffer[4];
    char buffer2[100];

    for (x = 0; x < NBYTES; x++)
    {
        octet[x] = (ipAddress >> (x * 8)) & (uint8_t)-1;
    }
    for (x = NBYTES - 1; x >= 0; --x)
    {
        // sprintf("%d", octet[x]);
        sprintf(buffer2, "%d", octet[x]);
        // if (x > 0) printf(".");
        if (x > 0) {
            char * temp = concat(buffer2, ".");
            strcpy(buffer2, temp);
        }
    }
    sprintf("\n%s", buffer2);
    return 0;
}

我正在尝试将此代码重写为一个函数,该函数连接 IP 的每个部分(使用 sprintf 从 32 位整数到 buffer2 char 变量),并返回带有 IP 十进制表示形式的转换后的字符串,但是我正在运行编译的应用程序,它打印错误:“总线错误:10”。

调试信息如下:

MacBook-Air-Pavel:gamind paus$ gcc -o int2ip int2ip.c 
int2ip.c:21:20: warning: implicitly declaring library function 'malloc' with type 'void *(unsigned long)'
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
                   ^
int2ip.c:21:20: note: please include the header <stdlib.h> or explicitly provide a declaration for 'malloc'
int2ip.c:21:27: warning: implicitly declaring library function 'strlen' with type 'unsigned long (const char *)'
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
                          ^
int2ip.c:21:27: note: please include the header <string.h> or explicitly provide a declaration for 'strlen'
int2ip.c:23:5: warning: implicitly declaring library function 'strcpy' with type 'char *(char *, const char *)'
    strcpy(result, s1);
    ^
int2ip.c:23:5: note: please include the header <string.h> or explicitly provide a declaration for 'strcpy'
int2ip.c:24:5: warning: implicitly declaring library function 'strcat' with type 'char *(char *, const char *)'
    strcat(result, s2);
    ^
int2ip.c:24:5: note: please include the header <string.h> or explicitly provide a declaration for 'strcat'
int2ip.c:50:21: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
    sprintf("\n%s", buffer2);
                    ^~~~~~~
/usr/include/secure/_stdio.h:47:56: note: expanded from macro 'sprintf'
  __builtin___sprintf_chk (str, 0, __darwin_obsz(str), __VA_ARGS__)
                                                       ^
5 warnings generated.

我不知道为什么。你能帮我吗?

最佳答案

sprintf("\n%s", buffer2);

那个sprintf是错误的;你需要交换两个参数。另外,请考虑自始至终使用 snprintf

<小时/>

此外,inet_ntop .

关于c - Mac 上连接字符集时出现总线错误 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28693762/

相关文章:

c++ - 从 std::string 获取第一个字符

c - 在C中打印字符串的地址是否有效

c++ - C/C++ : Naming conventions for arrays?

c++ - 多维数组迭代效率

c# - 使用 c# .net 将数组插入到 sql server 和 mysql 数据库中

python - 使用 Python/NumPy 对图像阈值进行矢量化

c - 初始化没有大小的数组字面量

c++ - 将字符串序列化为 UTF 无 BOM

c - 如何使用 pwm atmega avr 增加亮度或调暗 LED

string - 为什么这是错误的!?生成字符串