-1

So I have a character array of fixed size:

char buff[100];

And I have a character pointer:

char *ptr;

Now, the buffer will be filled partially or fully. I want to copy the contents of the buffer to ptr. How do I do that?

Thanks

Update:

int rcd; // Received bytes
int temp = 0; // This is used as a size to realloc the dataReceived character pointer
int packetLength = 0; // This is the total packet length
int *client = (int*) data;
int cli = *client; // The client socket descriptor
char buff[100]; // Buffer holding received data
char *dataReceived = malloc(0);

while ((rcd = recv(cli, buff, 100, MSG_DONTWAIT)) > 0) 
{
    dataReceived = realloc(dataReceived, rcd + temp + 1); // Realloc to fit the size of received data
    strcat(dataReceived, buff); // Concat the received buffer to dataReceived
    temp = rcd;
    packetLength = packetLength + rcd;
    memset(buff, 0, 100); // Reinitialize the buffer for the next iteration
}
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
user3266083
  • 103
  • 3
  • 12
  • 1
    This is very incomplete! Where is the code? You need to show what `ptr` points to! And it's also important What are the contents of `buff`? – Iharob Al Asimi Nov 12 '15 at 02:13
  • [`strcpy`](http://en.cppreference.com/w/c/string/byte/strcpy) – Bryan Chen Nov 12 '15 at 02:15
  • @BryanChen If `ptr` points to valid memory and `buff` is a `nul` terminated sequence, that's why I say there is a lot missing in this question. – Iharob Al Asimi Nov 12 '15 at 02:16
  • so I'm using buff to read data coming in from a socket. And atm I have char *ptr = malloc(0). I want to realloc ptr when the data comes in and copy data from buff to ptr. Does that make sense? – user3266083 Nov 12 '15 at 02:19
  • Yeah. Without further defining ' filled partially or fully.', no safe answer is possible. – Martin James Nov 12 '15 at 02:19
  • ...so, the socket read/recv returned a byte count, then? – Martin James Nov 12 '15 at 02:20
  • @MartinJames yes it did. – user3266083 Nov 12 '15 at 02:22
  • @iharob Added the code – user3266083 Nov 12 '15 at 02:29
  • @user3266083 `malloc(0)`? What? – Iharob Al Asimi Nov 12 '15 at 02:29
  • `dataReceived = realloc(dataReceived, rcd + temp + 1);` generally bad even if you have `64`GB of RAM. `strcat(dataReceived, buff);` very likely wrong because data sent through a socket could or not be `nul` terminated (i.e. A string). – Iharob Al Asimi Nov 12 '15 at 02:30
  • @iharob So how do I store the entire packet data when I don't know its size? So for example, in the while loop above, when it finishes reading data of an entire packet, its going to pop out of the loop. Now if the packet size > buff its going to overwrite the buffer. So when it overwrites, I'm gonna loose data if I do not save it to some other location. So how do I do that? How do I store data to some other location so that even if buff gets overwritten I still have the old data. And then I can simply concat the new data. – user3266083 Nov 12 '15 at 02:34
  • You do know the size!!!!!!!! What do you think the return value of [`recv(2)`](http://man7.org/linux/man-pages/man2/recvmsg.2.html) is? And AFAIK `malloc(0)` is UNDEFINED BEHAVIOR! Not sure, but I think it is - Correction, it's [implementation defined](http://stackoverflow.com/a/10684612/1983495). – Iharob Al Asimi Nov 12 '15 at 02:36
  • @iharob You are right. I suck! Sorry my bad. So should I just malloc the return value of recv to dataReceived and strcpy buff to it? – user3266083 Nov 12 '15 at 02:40
  • You mean you want to copy the buffer's contents *to the buffer ptr points to*, right? Not to ptr itself, that would make no sense. – user253751 Nov 12 '15 at 02:41
  • @immibis Yes that's correct – user3266083 Nov 12 '15 at 02:42
  • Not `strcpy()` -- `memcpy()` because you know the length! And you can ensure that it's`nul` terminated, also you would not need `strcat()` which is the worst c library function ever. – Iharob Al Asimi Nov 12 '15 at 02:47
  • @iharob Perfect! Thanks a lot man! Really appreciate it. :) – user3266083 Nov 12 '15 at 02:50
  • @iharob you sure? I would have gone with strtok()..... :) – Martin James Nov 12 '15 at 10:12
  • @MartinJames, when it's about strings in c, you should always remember that library functions will all search for the `nul` terminating byte, as long as it's possible to avoid that you improve your program's performance. – Iharob Al Asimi Nov 12 '15 at 11:26

2 Answers2

2

To copy the contents of buffer to wherever ptr points to, use:

memcpy(ptr, buffer, sizeof(buffer));

The first argument to memcpy is the destination, second is the source (same order as strcpy and strcat), and the third is the number of bytes to copy.

user253751
  • 57,427
  • 7
  • 48
  • 90
0

So it will be like

char buff[100]; // Buffer holding received data
char *ptr;
memcpy(ptr, buffer, sizeof(buffer));

right ? also if you want to free the buffer we can use

free(buffer);

right?

NIKHIL PUNNOOSE
  • 129
  • 1
  • 6