使用calloc

时间:2016-01-31 03:23:27

标签: c arrays string binary calloc

我留下了一些代码,但基本上我有一个小数组数组,我正在尝试将其转换为二进制数,但是作为一个字符串数组。对C来说是全新的,现在真的抓住了稻草。我不确定如何使用malloc和calloc。这是我尝试将其用于第一个数字/二进制字符串的尝试:

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

void binstringconvert (unsigned long int *decimals, char **binarystrings);


int main (int argc, char **argv) 
{
    // initialize variables
    int numLines = 9;
    int binaryLength = 32;
    unsigned long int decimals[numLines];   
    decimals[0] = 3241580200;   

    // convert decimal array to 32-bit binary string array
    char **binarystrings = calloc (numLines, binaryLength);
    binstringconvert(decimals, binarystrings);  

    // test print
    printf("\n\n%lu in binary number system is: ", decimals[0]);
    printf("\n%s", binarystrings[0]);   
}

void binstringconvert (unsigned long int *decimals, char **binarystrings)
{
    int c, k;

    for (c = 31; c >= 0; c--)
    {
        k = decimals[0] >> c;    
        if (k & 1)
            binarystrings[0][c] = '1';
        else
            binarystrings[0][c] = '0';
    }       
}

我是否正确初始化了binarystrings?我能按照我想要的方式写出个别角色吗?目前它给了我一个段错误。

2 个答案:

答案 0 :(得分:1)

您需要为指向字符串的指针数组以及字符串本身分配空间。第一次调用calloc()分配指向字符串的指针数组,每个malloc()分配字符串本身:

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

void binstringconvert (unsigned long int *decimals, char **binarystrings);


int main (int argc, char **argv) 
{
    // initialize variables
    int numLines = 9;
    int binaryLength = 32;
    unsigned long int decimals[numLines];   
    decimals[0] = 3241580200;
    int i;

    // Allocate an array of numLines pointers to strings
    char **binarystrings;
    if ( (binarystrings = calloc(numLines, sizeof(*binarystrings))) == NULL )
      return EXIT_FAILURE;

    // Allocate space for each string
    // binaryLength bits plus one for null terminator
    for ( i=0; i<numLines; ++i ) {
      if ( (binarystrings[i] = malloc((binaryLength + 1)*sizeof(**binarystrings))) == NULL )
        return EXIT_FAILURE;
    }

    // convert decimal array to 32-bit binary string array
    binstringconvert(decimals, binarystrings);  

    // test print
    printf("\n\n%lu in binary number system is: ", decimals[0]);
    printf("\n%s", binarystrings[0]);   
}

void binstringconvert (unsigned long int *decimals, char **binarystrings)
{
    int c, k;

    for (c = 31; c >= 0; c--)
    {
        k = decimals[0] >> (31 - c);    
        if (k & 1)
            binarystrings[0][c] = '1';
        else
            binarystrings[0][c] = '0';
    }
    binarystrings[0][32] = '\0';
}

另外,你没有在字符串的末尾写一个空终止符。当你尝试写它(printf("\n%s", binarystrings[0]);)时,它将继续通过内存读取,超过你希望结束的位置。在末尾写入零(&#39; \ 0&#39;)需要在分配中添加一个字符,因此+1调用中的malloc()

请注意使用sizeof()运算符。你永远不应该硬编码内存大小。在C中,让编译器弄清楚有多大的东西,不要试图猜测。指针在32位系统上可以是4个字节,在64位系统上可以是8个字节。即使您知道自己拥有什么系统以及它有多大,也可以使用sizeof()

编辑:@DavidCRankin注意到的更正逻辑错误:

        k = decimals[0] >> (31 - c);    

答案 1 :(得分:1)

我道歉,我解释说你想要将多个数字传递给你的函数并将它们全部转换为二进制字符串并返回。无论如何,这个例子仍然有效。在任何一种情况下,要将32位数字拟合到一个字符串中,您将需要33个字符( nul-terminatedating 字符为+1)。另外,您正在以相反的顺序编写二进制字符串。

只需进行一些调整即可纠正订单。例如:

# include <stdio.h>
# include <stdlib.h>
# include <string.h>

enum { DWRD = 32 };

void binstringconvert (unsigned *decimals, char (*binarystrings)[DWRD+1], int n);

int main (void)
{
    /* initialize variables */
    unsigned int decimals[] = { 1, 255, 65535, 8388607,
                                3241580200, 2898560974,
                                4294967295, 3097295382,
                                1076482445, 1234567890 };
    char (*binarystrings)[DWRD+1] = {NULL};
    int i, n = sizeof decimals/sizeof *decimals;

    binarystrings = calloc (n, sizeof *binarystrings);
    binstringconvert (decimals, binarystrings, n);

    /* test print */
    for (i = 0; i < n; i++)
        printf (" %10u : %s\n", decimals[i], binarystrings[i]);

    free (binarystrings);

    return 0;
}

void binstringconvert (unsigned *decimals, char (*binarystrings)[DWRD+1], int n)
{
    int c, i, k;
    for (i = 0; i < n; i++) {
        for (c = 31; c >= 0; c--)
        {
            k = decimals[i] >> c;
            if (k & 1)
                binarystrings[i][31-c] = '1';
            else
                binarystrings[i][31-c] = '0';
        }
        binarystrings[i][DWRD] = 0;  /* nul-terminate */
    }
}

<强>输出

$ ./bin/binstrings
          1 : 00000000000000000000000000000001
        255 : 00000000000000000000000011111111
      65535 : 00000000000000001111111111111111
    8388607 : 00000000011111111111111111111111
 3241580200 : 11000001001101101001011010101000
 2898560974 : 10101100110001001000011111001110
 4294967295 : 11111111111111111111111111111111
 3097295382 : 10111000100111001111101000010110
 1076482445 : 01000000001010011101000110001101
 1234567890 : 01001001100101100000001011010010