Syntax for std::fread :
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream );
we can call std::fread with a character pointer and get the read data with the same character pointer. eg:
char* data;
fread(data,5,10,file);
What i tried is to create a similar function which accepts a character pointer as a void* pointer.
#include <iostream>
#include<string.h>
using namespace std;
void modify(void* ptr)
{
char* temp = new char[50];
strcpy(temp,"testing");
__ptr=(void*)temp;
std::cout<<"__ptr="<<(char*)__ptr<<endl;
}
int main()
{
char* str;
modify(str);
cout<<"str="<<str;
return 0;
}
I tried the above code with http://www.compileonline.com/compile_cpp11_online.php and I got the output as
__ptr=testing
str=
(a)Why str is not printing the string "testing"?
I am assigning the address of a dynamically allocated variable to ptr. So the value "testing" must be available in the heap even after the control returns from the method.
(b)How can i modify the function so that i will get the output as
__ptr=testing
str=testing
But function prototype cannot be modified.
Please help.
Hope the question is clear.
C, if you want to do things theC++way, you should use iterators and streams.char*to be called. If this compiles for you, kindly tell us what your toolchain is (and if it is TurboC++ I won't be a bit surprised at the crap it accepts).