4
Contributors
6
Replies
7
Views
9 Years
Discussion Span
Last Post by Radical Edward
0

Do you mean something like

#include <stdio.h>

int main ()
{
  char* first = "first";
  char* second = "second";
  first = second;
  printf("%s\n%s\n", first, second);
  printf("address of first: %d\naddress of second: %d\n", &first, &second);
  getchar();
  return 0;
}
0

If you want to make a completely unique copy of the data then you need to know the number of bytes as well as have a pointer to the data. Then you can allocate enough memory to the new pointer and manually copy the data:

unsigned char newData = malloc(size);

if (newData == NULL)
  /* Out of memory error */

memcpy(newData, data, size);
0

Thank you for reply... but my question is different.. my pointer is void pointer(void *) and it may be RAW data..it could be anything. but sure tht it is not string. waiting 4 reply..


Do you mean something like

#include <stdio.h>

int main ()
{
  char* first = "first";
  char* second = "second";
  first = second;
  printf("%s\n%s\n", first, second);
  printf("address of first: %d\naddress of second: %d\n", &first, &second);
  getchar();
  return 0;
}
0

Thank you for reply... but my question is different.. my pointer is void pointer(void *) and it may be RAW data..it could be anything. but sure tht it is not string. waiting 4 reply..

Like Edward already showed, you need to know the size of the memory block to copy, then use malloc() to allocate a buffer large enough, and finally use memcpy() to copy the data to the allocated buffer.

0

Thnx all, for ur quick replies...Till now, u help me very nice. But there is another case.... If there is no way to calculate the size of RAW data[bcoz it may be changing every time execution]..then wht would I do? Is there an another way?

0

> If there is no way to calculate the size of RAW data..then wht would I do?
You should always have the size, either because you allocated enough memory to store the data, counted the data at some point, or the size was given to you by code that did one of the first two things. If you don't have a way to get the size, that represents a design flaw and needs to be fixed.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.