如何在C中将char*复制到char*数组

如何在C中将char*复制到char*数组,c,arrays,C,Arrays,我试图生成uint_32随机数,这些随机数应该存储在缓冲区数组中,但不知何故,我的代码每次只存储最后一个值 例如,当我生成随机数时 12365645 97897875 45458788 那么 buffer[0]=12365645 buffer[1]=97897875 buffer[2]=45458788 但是现在,我变得 buffer[0]=45458788 buffer[1]=45458788 buffer[2]=45458788 这是我对应的代码,但我不确定我在哪里犯了错误 /*Req

我试图生成uint_32随机数,这些随机数应该存储在缓冲区数组中,但不知何故,我的代码每次只存储最后一个值

例如,当我生成随机数时

12365645
97897875
45458788
那么

buffer[0]=12365645
buffer[1]=97897875
buffer[2]=45458788
但是现在,我变得

buffer[0]=45458788
buffer[1]=45458788
buffer[2]=45458788
这是我对应的代码,但我不确定我在哪里犯了错误

/*Required header files are added*/
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>         
#include <fcntl.h>
#include <stdint.h>

struct thread_arguments
{
    char *buffer;
    char *queue[10];
    uint32_t offset;
    uint32_t r;
    size_t bufferlen;


    size_t minlevel;


}ta;

void randomgenerate();
void constructor(int size, int filllevel);
void put_buffer(int ele);

void printbuf();
int main(void)
{
ta.offset=0;
ta.buffer=NULL;

    constructor(1,3);
    randomgenerate();
    printbuf();

    return EXIT_SUCCESS;

}
void constructor( int filllevel,int size)
{   
    //ta.buffer[size];
     ta.bufferlen=size;  
     ta.minlevel=filllevel;
}

void randomgenerate()
{
int i;
    for(i=0;i<ta.bufferlen;i++)
    {
                int myFile = open("/dev/random", O_RDONLY);             
                read(myFile, &ta.r, sizeof(ta.r)) ;

                put_buffer(i);
                close(myFile);

    }
}
void put_buffer(int ele)
{


ta.buffer = realloc(ta.buffer, sizeof(uint32_t));
sprintf(ta.buffer, "%zu", ta.r);
ta.offset += sizeof(uint32_t);
ta.queue[ele]=ta.buffer;
printf("%d\t%s\n",ele,ta.queue[ele]);

}
void printbuf()
{
int k;

    for(k=0;k<ta.bufferlen;k++)
    {
    printf("%s\n",ta.queue[k]);
    }
}
添加了所需的头文件*/ #包括 #包括 #包括 #包括 #包括 #包括 结构线程参数 { 字符*缓冲区; 字符*队列[10]; uint32_t偏移量; uint32_t r; 大小缓冲区; 尺寸最小水平; }ta; void随机生成(); void构造函数(int size,int filllevel); 无效输入缓冲区(int ele); void printbuf(); 内部主(空) { ta.偏移量=0; ta.buffer=NULL; 建造师(1,3); 随机生成(); printbuf(); 返回退出成功; } void构造函数(int filllevel,int size) { //缓冲区[大小]; ta.bufferlen=大小; ta.minlevel=filllevel; } void random生成() { int i;
对于(i=0;i您误用了
realloc()
。您的
ta.buffer
总是指向相同的
ta.buffer
,因为它已经等于
sizeof(uint32_t)
。因此,当您执行赋值
ta.queue[ele]=ta.buffer
,每个
ta.queue
指针都是相同的值

您需要的是
malloc()

编辑

每次调用
put\u buffer()
,都应该使用
malloc()
,如下所示:

void put_buffer (int ele)
{
    /* There are 10 decimal digits (characters) in a 32-bit unsigned integer, + 1 for the null terminator */
    ta.buffer = malloc ((sizeof *ta.buffer) * 11); 
    /* Print the string representation to the newly-allocated buffer */
    sprintf(ta.buffer, "%u", ta.r);
    /* I'm not sure what this is for so I'll leave it alone */
    ta.offset += sizeof (uint32_t);
    ta.queue[ele] = ta.buffer;
}

您不仅每次使用的
realloc
大小相同,而且分配的内存量也不正确

 ta.buffer = realloc(ta.buffer, sizeof(uint32_t));
 sprintf(ta.buffer, "%zu", ta.r);
sizeof(uint32\u t)
将是
4
。内存不足,无法以字符串形式存储
uint32\u t
类型的对象


你的程序有未定义的行为。

如果不阅读你的代码,我打赌
缓冲区的所有元素都指向同一个存储位置。程序编译了吗?我得到一个错误:64:33:错误:从“void*”到“char*”的转换无效。你可能希望查看
realloc()的使用情况并考虑一个不同的函数是否更适合您想要的。您需要为每个<代码> TA.Que[eLe] < /C>使用简单的<代码> MalCube()/<代码>分配空间。为什么不在<构造函数> <代码> MalOC/<代码>,然后将MeMSET设置为0?您当前没有“重新分配”。不管怎样,您只是将缓冲区的大小调整为当前大小。我已扩展了我的答案,以包括示例用法。您的大小不正确。您正在为
ta.buffer
中的32位无符号整数的字符串表示腾出空间,因此您需要考虑所需的字符数。3的大小不足以表示字符字符以适合字符串“4294967295”。