0

How it's possible that we are storing some function in pointer's like win = SDL_CreateWindow("Hello World", posX, posY, width, height, 0); ? If it's not a function so what is it cuz it looks like a function. Second what type of pointer's are these is it pointer to member function ? I mean that SDL_Window *win; SDL_Renderer *renderer;. How it's possible that I can just declare it like that without any ampersand ? Can you text me pseudocode to understand how this pointer can hold this "function"?

include "SDL.h"

int main(int argc, char *argv[])
{
    int posX = 100, posY = 100, width = 320, height = 240;
    SDL_Window *win;
    SDL_Renderer *renderer;
    win = SDL_CreateWindow("Hello World", posX, posY, width, height, 0);
    renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
Virozz
  • 69
  • 5
  • 2
    Which language, C **or** C++? They are distinct languages. For example, C++ has smart pointers and `std::string`. The C language doesn't. Please update your language tags appropriately. – Thomas Matthews Aug 11 '20 at 00:13
  • 1
    `SDL_CreateWindow` returns a pointer. All of the getting of the address happened inside the function. No `&` required. `&` is needed to get the address of a variable, but if that variable is already a pointer, `&` isn't needed. Unless you want to get a pointer to the pointer... – user4581301 Aug 11 '20 at 00:13
  • 3
    I recommend consulting an introductory text on programming C or C++. The first few chapters should be able to help you out far better than the Internet can. – user4581301 Aug 11 '20 at 00:14
  • 4
    `How it's possible that we are storing some function in pointer's like` [Here is a good list of C books](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). Please re-read the chapters related to functions. Stackoverflow is a bad place to learn programming, rather it's a place for people that already know programming that want to solve interesting problems. – KamilCuk Aug 11 '20 at 00:15
  • A few links that provide basic discussions of pointers may help. [Difference between char *pp and (char*) p?](https://stackoverflow.com/a/60519053/3422102) and [Pointer to pointer of structs indexing out of bounds(?)...](https://stackoverflow.com/a/60639540/3422102) – David C. Rankin Aug 11 '20 at 00:56
  • @KamilCuk That's a bad list of random books, including lots of bad books... https://meta.stackoverflow.com/questions/355588/the-c-book-list-has-gone-haywire-what-to-do-with-it – Lundin Aug 11 '20 at 06:55

1 Answers1

1

Actually win is not pointer to a function but it is a pointer to structure type SDL_Window. Statement win = SDL_CreateWindow("Hello World", posX, posY, width, height, 0) means that you have calling function SDL_CreateWindow with arguments ("Hello World", posX, posY, width, height, 0) this function as the result of arguments used returns a pointer to dynamically allocated inside this function structure SDL_Window.

Function pointer can be declared with the following construction:

SDL_Window * (*functionName)(const char*, int, int, int, int, int) = SDL_CreateWindow;

Than used like that:

win = functionName("Hello World", posX, posY, width, height, 0);

functionName in this case points to SDL_CreateWindow function and is used to call SDL_CreateWindow.

Mykola
  • 3,343
  • 6
  • 23
  • 39
  • OP is clearly confused by basic use of pointers. Though correct, showing a function pointer is overly confusing at this point. – Mooing Duck Aug 11 '20 at 00:59