Hi all,
I have one question regarding pointers..

suppose..I've one pointer tht points to block of data[RAW data - may be sound file]. and i want to copy this data to another location which pointed by another pointer. so plz show me the way ..how to do tht.

Recommended Answers

All 6 Replies

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;
}

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);

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;
}

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.

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?

> 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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.