multidimensional arrays and transferring buffers

前端 未结 2 1445
情深已故
情深已故 2021-01-29 12:23

I am trying to save a buffer into array segments of 1800 characters. When ever I am able to transfer the buffer over but when I try to print each new buffer out. I get null for

相关标签:
2条回答
  • What do you expect this to do?

                    printf("KEY%d DATA:%.*s\n",count,msg_buff[count][0],1800);
    

    msg_buff[count][0] is the first character of your array. You need to pass msg_buff[count] to print the whole string.

    Also, the * modifier is defined as The width is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted. I would interpret this to mean that your 3rd argument (1800) must precede your second argument (msg_buff[count]). To make things simple, make sure all of your strings are NULL-terminated and don't use the width modifier.

    To actually create your array of keys, don't copy each character one by one in the loop. Use strncpy instead. Ensure that you have space in the buffer for the NULL-terminator (allocate 1801 bytes rather than 1800). Right now your copy loop is broken anyway, because you never increment cbuf_pos, so every character is written to the first position in the array.

    0 讨论(0)
  • 2021-01-29 13:21

    Where are you incrementing the cbuf_pos, when assigning to the msg_buff? Also, in the if(buf_pos % 1800 ==0), terminate the msg_buff[key_num][cbuf_pos] with a '\0' (checking that there is one more character left).

    0 讨论(0)
提交回复
热议问题