Last active
April 30, 2025 18:47
-
-
Save sunmeat/a44f6ef01c76f2cf1a071764937c6a6d to your computer and use it in GitHub Desktop.
вказівник на невизначений тип void*
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void swap(void *a, void *b, size_t size) | |
{ | |
char *tmp = (char*) malloc(size); | |
memcpy(tmp, a, size); | |
memcpy(a, b, size); | |
memcpy(b, tmp, size); | |
free(tmp); | |
} | |
int main() | |
{ | |
setlocale(0, "UKR"); | |
system("title Pointers"); | |
int i = 10; | |
double d = 3.14; | |
double *pa, *pb, pc; // pc - звичайна змінна, а не покажчик! | |
double *pd = &d; | |
// int *pi = &d; // error C2440: cannot convert from 'double*' to 'int*' | |
void *pv = &i; // покажчик на невизначений (будь який) тип | |
cout << *((int*)pv) << "\n"; | |
pv = &d; | |
cout << *((double*)pv) << "\n"; | |
////////////////////////////////////////////////////////// | |
int b = 15; | |
swap(&i, &b, sizeof(int)); | |
cout << i << " " << b << "\n"; | |
double f = 2.61; | |
swap(&d, &f, sizeof(double)); | |
cout << d << " " << f << "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment