Copying a char* into char*

Hello,
I am trying to copy an unsigned char* into an unsigned char*, but getting core dump. I tried with memcpy and strcpy as well. Not sure about the syntax. Please help. Here is a small sample code snippet.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
using namespace std;


void function (unsigned char* digits)
{

unsigned char* inTake = NULL;

//strcpy((char*)inTake,(const char*)digits); - How to do it?
//Segmentation fault

}
int main()
{
unsigned char digits [20]= "7709916089";
unsigned char number[20];

memcpy((void*)number,(void*)digits,sizeof(number));

function(number);

return 0;
}
I think you must add a null-terminating character "/0".
This must help: http://www.cplusplus.com/reference/cstring/strcpy/

Thanks,
Aceix.
You did not allocate memory where you are going to copy the source string. You defined a pointer that has value NULL.

unsigned char* inTake = NULL;

You may not copy to the memory with address 0.
@vlad - Oops. You are right. Not even sure why did I do that :S Thanks, It now works.

@Aceix - There was a problem with memory allocation. Thanks for your help and link :)
Topic archived. No new replies allowed.