Int to char数组作为函数以简单的方式返回char数组

时间:2017-01-27 04:06:06

标签: c arrays

我一直在互联网上寻找这个,到目前为止我只是找到了很多问题,而不是一般的答案。

我在C上生锈了。我想创建一个返回char数组的函数。

这是我得到的,不起作用。基本上是一种将字节数组转换为字符数组的方法,以便稍后进行atoi ..

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

char *get_char(int my_byte[], int packetsize)
{
    char *array_char=(char *) malloc(sizeof(char)*10);  //trying this but didnt work
     // char array_char[10]; //i had it like this before(was told to do it)

        for(int i=0;i<10;i++)
        {
            array_char[i]=my_byte[i]+0;
        }           

        return array_char;
    }

int main()
{

    int byte_array[]={1,2,3,4,5,6,7,8,9,0};
    char *temp;
    char data;

    temp=get_char(byte_array,10);   
    data=*temp;
    printf("String  point %s ",data);

}

3 个答案:

答案 0 :(得分:2)

两个修正:

  • 如果您想转换为char,那么

array_char[i]=my_byte[i]+0;应为array_char[i]=my_byte[i]+'0';注意'0'是字符(将转换为int)而不是数字0(不执行任何操作)。

  • 此外,您必须在temp中释放main指针,因为该内存是在get_char()函数中动态分配的。

修改:只需注意get_char()

中的其他问题
char *array_char=(char *) malloc(sizeof(char)*10);

应该是

char *array_char= malloc(sizeof(char)*(packetsize+1));

for循环之后,确保缓冲区已终止NUL:

array_char[packetsize] = '\0';

请注意,您的packetsize从未使用过 - 您应该收到一些编译器警告。在10中硬编码malloc是不好的 - 实际上是将packetsize解析为参数的整个想法 - 所以正确使用它。

答案 1 :(得分:1)

你需要注意这些事情:

  • 您需要在*array_char的末尾添加一个空终止字符,否则使用从堆分配的指针将导致undefined behaviour
  • 您可以像这样简单地分配*array_char

    char *array_char = malloc(packetsize+1);
    

    sizeof(char)1+1为尾随nullbyte。

  • 您也不需要cast return of malloc()

  • 您应该将此大小作为10传递,而不是将packetsize作为get_char()传递给sizeof(arr) / sizeof(arr[0],这是计算出的数组大小。这可以是在某处声明的size_t变量,甚至是宏。
  • 需要检查
  • malloc(),因为如果不成功,它可以返回NULL
  • 您需要在该计划的某个时刻free() temp
  • array_char[i]=my_byte[i]+0;需要array_char[i]=my_byte[i]+'0';,因为'0'是零字符的ascii代码。

  • char data必须为char *data,因为temp是指针。

    如果使用-Wall -Wextra进行编译,您将看到以下行:

    data=*temp;
    

    很危险,并会触发警告,在没有演员表的情况下从整数制作指针。它很可能会导致分段错误。如果tempdata都是指针,那么您只需使用:

    data=temp;
    

    data设置为temp的地址。有时这被写为data = &(*temp);,但这很难阅读。虽然它们不需要data,但单独使用temp应该没问题。

您的代码可能如下所示:

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

#define ARRAYSIZE(arr) (sizeof(arr) / sizeof(arr[0]))

char *get_char(int my_byte[], size_t packetsize) {
    char *array_char = malloc(packetsize+1);
    const char ascii = '0';
    size_t i;  

    if (!array_char) {
        printf("Cannot allocate %zu bytes\n", packetsize+1);
        exit(EXIT_FAILURE);
    }

    for(i = 0; i < packetsize; i++) {
        array_char[i] = my_byte[i] + ascii;
    }
    array_char[i] = '\0'; /* or array_char[packetsize] = '\0' */           

    return array_char;
}

int main(void) {
    int byte_array[]={1,2,3,4,5,6,7,8,9,0};
    char *temp, *data;

    temp = get_char(byte_array, ARRAYSIZE(byte_array));  
    data = temp;
    printf("String  point %s\n", data);

    printf("String converted into number = %d\n", atoi(data));

    free(temp);
    temp = NULL;

    return 0;
}

您还可以查看strtol,这比在错误检查方面使用atoi()要好。

答案 2 :(得分:0)

从函数返回数组并不明智。那么如何返回一个字符串呢?由于大多数libc函数都使用,我们可以使用类似的东西(即)将缓冲区与我们的输入一起传递,并期望函数使用输出缓冲区来为我们提供结果。

编码时需要注意的一些问题

  1. 首先写下你的逻辑。
  2. 尝试使用libc中的可用功能。
  3. 在处理字节数据/二进制数据时,应采取预防缓冲区溢出的方法。
  4. 不要在函数中分配并在另一个函数中取消分配。
  5. 以下是修改后的代码示例。

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <stdint.h>
    
    int get_char(uint8_t my_byte[], int packetsize, char *buffer, int max_buffer)
    {
            int byte_itr, buf_itr;
            char temp_buf[16]={0x00};
            for(byte_itr=0, buf_itr=0; byte_itr<packetsize && max_buffer > buf_itr; byte_itr++)
            {
                    memset(temp_buf, 0x00, sizeof(temp_buf));
                    char temp_ch = my_byte[byte_itr];
                    snprintf(temp_buf, sizeof(temp_buf), "%d", temp_ch);
                    if( buf_itr+strlen(temp_buf) >=max_buffer){
                            break;
                    }else{
                            buf_itr += strlen(temp_buf);
                            strcat(buffer, temp_buf);
                            if(byte_itr+1 < packetsize){
                                    strcat(buffer, ",");
                                    buf_itr += 1;
                            }
                    }
            }
    
            return buf_itr;
    }
    
    int main()
    {
            uint8_t byte_array[]={1,2,3,4,5,6,7,8,9,0};
            char char_array[32]={0x00};
            int len = get_char(byte_array, 10, char_array, sizeof(char_array));
            printf("String  point %s : len %d\n", char_array, len);
    
    }
    

    注意: 当长度返回和输出缓冲区大小相同时,缓冲区满状态就会发生。