Комментарий #11352701

kotelric
в топике c cheatsheets
i always forget how pointers acutally work so here's a cheatsheet:
int m = 400;
int *ptr = &m; // that means you declared a pointer that stores a memory address of m 
printf("%p", ptr) // %p stands for pointer, and the syntax here says to dereference the pointer, to get the value from the address that the pointer held.
print("%p", &ptr) // &ptr is the address where to ptr pointer is stored. also works with usual variables like m, so &m would print the address to the m variable
**ptr is a pointer to a pointer
*char ptr and char *ptr are identical
also, sometimes you'll need to declare a pointer and use it as a variable, like here for example:
GLFWwindow* window = glfwCreateWindow(640, 480, "Triangles", NULL, NULL);
you need to declare a pointer instead of regular variable here because the function glfwCreateWindow returns a pointer itself.
a data structure can be a pointer itself 
Ответы
kotelric
kotelric#
@kotelric, another thing about pointers: sometimes they are a child or a parent object of a struct, a class. so you can use -> for dereferncing child objects of what you just dereferenced.

here's an example:
    if (device) {
        device->lpVtbl->Release(device);
    }

the device variable holds a pointer to a COM object, and every COM object has a Release() method to clean up the object from memory. considering it's a COM object, you need to dereference lpVtbl, which is a pointer to a table with lots and lots of COM functions, and then dereference Release() from that table.
назад
Твой комментарий
Вернуться к редактированию
Предпросмотр
Скрыть