将1D字符*划分为2D字符** [英] Partition a 1D char* into 2D char**

查看:97
本文介绍了将1D字符*划分为2D字符**的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于将2D数组转换为1D数组有很多问题,但我正尝试相反的尝试。我正在尝试将一个字符串划分为恒定长度的子字符串,并将其放置在2D数组中。此2D矩阵的每一行都应包含初始字符串的子字符串,并且,如果要连续读取并连接每一行,则应复制初始字符串。

There are a lot of questions about converting a 2D array into a 1D array, but I am attempting just the opposite. I'm trying to partition a string into substrings of constant length and house them in a 2D array. Each row of this 2D matrix should contain a substring of the initial string, and, if each row were to be read in succession and concatenated, the initial string should be reproduced.

我几乎可以使用它了,但是由于某种原因,我丢失了初始字符串(bin)的第一个子字符串(分区[0]-长度8 * blockSize):

I nearly have it working, but for some reason I am losing the first substring (partitions[0] -- length 8*blockSize) of the initial string (bin):

int main (void){
    char* bin = "00011101010000100001111101001101000010110000111100000010000111110100111100010011010011100011110000011010";
    int blockSize = 2; // block size in bytes
    int numBlocks = strlen(bin)/(8*blockSize); // number of block to analyze
    char** partitions = (char**)malloc((numBlocks+1)*sizeof(char)); // break text into block
    for(int i = 0; i<numBlocks;++i){
        partitions[i] = (char*)malloc((8*blockSize+1)*sizeof(char));
        memcpy(partitions[i],&bin[8*i*blockSize],8*blockSize);
        partitions[i][8*blockSize] = '\0';
        printf("Printing partitions[%d]: %s\n", i, partitions[i]);
    }
    for(int j=0; j<numBlocks;++j)
        printf("Printing partitions[%d]: %s\n", j,partitions[j]);
    return 0;
}

输出如下:

Printing partitions[0]: 0001110101000010
Printing partitions[1]: 0001111101001101
Printing partitions[2]: 0000101100001111
Printing partitions[3]: 0000001000011111
Printing partitions[4]: 0100111100010011
Printing partitions[5]: 0100111000111100
Printing partitions[0]: Hj
Printing partitions[1]: 0001111101001101
Printing partitions[2]: 0000101100001111
Printing partitions[3]: 0000001000011111
Printing partitions[4]: 0100111100010011
Printing partitions[5]: 0100111000111100

在第一个for循环中成功构建分区。读取后进行构造后,partitions [0]处的字符串包含垃圾值。谁能提供一些见识?

The construction of partitions in the first for loop is successful. After construction at read out, the string at partitions[0] contains garbage values. Can anyone offer some insight?

推荐答案

他们的代码存在一些问题。

Their are a few problems with your code.


  • 您分配的 **分区错误。

而不是:

char** partitions = (char**)malloc((numBlocks+1)*sizeof(char)); /* dont need +1, as numblocks is enough space. */

您需要为 char * 指针,而不是 char 个字符。

You need to allocate space for char* pointers, not char characters.

相反,它需要是:

char** partitions = malloc((numBlocks+1)*sizeof(char*));


  • 也请阅读为什么不强制转换malloc()的结果,因为在C语言中不需要。

  • Also read Why not to cast result of malloc(), as it is not needed in C.

    malloc(),因为不成功时它可能返回 NULL

    malloc() needs to be checked everytime, as it can return NULL when unsuccessful.

    以下代码显示了此内容:

    Here is some code which shows this:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define BLOCKSIZE 2
    #define BLOCK_MULTIPLIER 8
    
    int main(void) {
        const char *bin = "00011101010000100001111101001101000010110000111100000010000111110100111100010011010011100011110000011010";
        const size_t blocksize = BLOCKSIZE;
        const size_t multiplier = BLOCK_MULTIPLIER;
        const size_t numblocks = strlen(bin)/(multiplier * blocksize);
        const size_t numbytes = multiplier * blocksize;
    
        char **partitions = malloc(numblocks * sizeof(*partitions));
        if (partitions == NULL) {
            printf("Cannot allocate %zu spaces\n", numblocks);
            exit(EXIT_FAILURE);
        }
    
        for (size_t i = 0; i < numblocks; i++) {
            partitions[i] = malloc(numbytes+1);
            if (partitions[i] == NULL) {
                printf("Cannot allocate %zu bytes for pointer\n", numbytes+1);
                exit(EXIT_FAILURE);
            }
            memcpy(partitions[i], &bin[numbytes * i], numbytes);
            partitions[i][numbytes] = '\0';
            printf("Printing partitions[%zu]: %s\n", i, partitions[i]);
        }
    
        printf("\n");
        for(size_t j = 0; j < numblocks; j++) {
            printf("Printing partitions[%zu]: %s\n", j,partitions[j]);
            free(partitions[j]);
            partitions[j] = NULL;
        }
    
        free(partitions);
        partitions = NULL;
    
        return 0;
    }
    

    输出非垃圾值:

    Printing partitions[0]: 0001110101000010
    Printing partitions[1]: 0001111101001101
    Printing partitions[2]: 0000101100001111
    Printing partitions[3]: 0000001000011111
    Printing partitions[4]: 0100111100010011
    Printing partitions[5]: 0100111000111100
    
    Printing partitions[0]: 0001110101000010
    Printing partitions[1]: 0001111101001101
    Printing partitions[2]: 0000101100001111
    Printing partitions[3]: 0000001000011111
    Printing partitions[4]: 0100111100010011
    Printing partitions[5]: 0100111000111100
    

    这篇关于将1D字符*划分为2D字符**的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆