Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Silly pointer question

Status
Not open for further replies.

SmileeTiger

Programmer
Mar 13, 2000
200
US
I am trying to do something with a pointer to an array of characters being passed to a function. More or less what I want to do is place the contents of the array into another array that I have created with a pointer pointing to it.

void Func(unsigned char *data, int dataWidth)
{
int width=100;
unsigned char *NewData= new unsigned char[width];
for (int i=0; i<dataWidth; i++)
*(NewData+i)=(data+i);

}

Should this work?


Smilee
 
I think you may have forgotten an asterisk:
*(NewData+i)=*(data+i);

If that doesn't work, you may wish to try replacing the &quot;for&quot; loop with the following line:

memcpy(NewData, data, dataWidth);
 
why not do this

strncpy(NewData,data,dataWidth);

could save you some time with the for loop.

Keiths post above will also produce the desired result but after the for loop you will need to do
*(NewData+dataWidth) = '\0';

Matt
 
Another tip would be using &quot;strdup(...)&quot; : it allocates the exact memory amount to store a null terminated character string an copy it.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top