Skip to content

Instantly share code, notes, and snippets.

@red-stripe
Created April 15, 2018 01:19
Show Gist options
  • Save red-stripe/54be5b6acf9a99d98176ae70b90488d1 to your computer and use it in GitHub Desktop.
Save red-stripe/54be5b6acf9a99d98176ae70b90488d1 to your computer and use it in GitHub Desktop.
void MyFunct(char* buffer) {
void* myvoid; // a pointer to void can point to anything
char* b = new char[20]; // char array
myvoid = b; // memory address at myvoid is the same as at b
strcpy((char*)myvoid, "this is text"); // memory address at myvoid is 't'
std::cout << (char*)myvoid << '\n'; //print the string stored at myvoid
buffer = NULL; // set the buffer pointer to NULL
}
int main(int argc, char const *argv[]) {
char* text = new char[20]; // initialise pointer to char of length 20
if(text) { // check if text is null
std::cout << "wow text is initialised to null" << '\n';
}
MyFunct(text);
if(text){ // null check
std::cout << "text was null" << '\n';
}
std::cout << text << '\n';
return 0;
}
/*
wow text is initialised to null
this is text
text was null
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment