-8

I'm studying C and know nothing about C++. LazyFoo's SDL2 tutorial that uses C++ does a thing with double colons that I don't understand and thus cannot follow in C.

If it helps, here's the link to the tutorial:

http://lazyfoo.net/tutorials/SDL/04_key_presses/index.php

SDL_Surface *loadSurface( std::string path )
{
    //Load image at specified path
    SDL_Surface *loadedSurface = SDL_LoadBMP( path.c_str());
    if( loadedSurface == NULL )
    {
        printf("Unable to load image %s! SDL Error: %s\n", path.c_str(), SDL_GetError());
    }

    return loadedSurface:
}

Everything here makes sense to me except the function parameters and LoadBMP parameters. I don't know what the :: means, and I don't know what path.c_str() is referring to.

Please, could someone explain it in a way that makes sense in C, or suggest a C-only workaround?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    That's a namespace, `::` being the scope operator. – Hatted Rooster Jun 27 '18 at 17:19
  • 9
    C and C++ are different programming languages (and much more different than their apparent similarity). If you don't want to learn C++ (which is a reasonable decision) don't try to read or use C++ code. There is no equivalent in C or C++ scope operator. – Basile Starynkevitch Jun 27 '18 at 17:19
  • 1
    :: is scope resolution. NameSpaces or Classes use these to remove ambiguity and to help hide names. Honestly, a 1 second google of this would explain it. the .c_str() call is converting a standard c++ string into a C string of which you are familiar. Again, a quick google explains. In todays world, not learning C++ at all is going to be a serious handicap - even in embedded land. You should do yourself a favor and at least become familiar with it. – Michael Dorgan Jun 27 '18 at 17:20
  • 2
    Scope resolution operator. Consider learning C++ from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of a random tutorial. – Algirdas Preidžius Jun 27 '18 at 17:20
  • @MichaelDorgan A 1 second google wouldn't explain namespaces, classes and member functions to someone who has never used a language providing those features. –  Jun 27 '18 at 17:21
  • First google: https://www.geeksforgeeks.org/scope-resolution-operator-in-c/ – Michael Dorgan Jun 27 '18 at 17:22
  • @MichaelDorgan Now put yourself in the position of someone who has never used C++. That link is not useful. –  Jun 27 '18 at 17:23
  • 1
    I appreciate where you are coming from. I was forced to learn C++ kicking and screaming from C as well. I would say even before I knew anything about C++, I could read the first sentence in that link and see what `::` does. `.c_str()` would require some more knowledge though. I do remember wondering why the hell people were putting functions inside of structures. :) – Michael Dorgan Jun 27 '18 at 17:25
  • I will be studying C++ next year but for my first year at university, C is where I'm at. I don't need a direct equivalent, but if any of you know SDL as well, just a different way of doing this would be helpful. My lecturer told me I don't need to know C++ to follow this tutorial so there should be some uncomplicated way around it? – Richard Robertson Jun 27 '18 at 17:30
  • Possible duplicate of [Why does C++ need the scope resolution operator?](https://stackoverflow.com/questions/9338217/why-does-c-need-the-scope-resolution-operator) –  Jun 27 '18 at 17:32
  • 1
    In C, to achieve similar (but by very different means, and many nuanced differences), I've seen developers use a "prefix-with-underscore" approach. For example, if I came out with the Next Big Thing library, I may have a `NBT_string` where the "NBT_" prefix would help to prevent collisions with your `RR_string`. SDL itself used "SDL_" prefix. – Eljay Jun 27 '18 at 17:35
  • 2
    Your lecturer says you don't need to know C++ to learn C, and Kate Gregory says you don't need to know C to learn C++ in her [lecture](https://www.youtube.com/watch?v=YnWhqhNdYyk). – Weather Vane Jun 27 '18 at 17:49
  • @RichardRobertson your question is already answered, but if you'll try to directly use said tutorials in C, things will explode in most unexpected ways, so don't be surprised. Quesion about `close` function in lazyfoo tutorials pops every now and then - it just not written with possible C collisions in mind, and beginners rarely have a grasp to see potential quirks. – keltar Jun 28 '18 at 05:57
  • @Keltar You're completely right. I encountered the 'close' issue as well. Simple enough just to change the name though. However from what I can tell, the lazyfoo tutorials are still the best free online SDL resource for C students. I'd kill for something this good writted in pure C. – Richard Robertson Jul 05 '18 at 18:43

2 Answers2

9

In C, loadSurface would be declared as simply

SDL_Surface *loadSurface(const char *path)

which means the call to SDL_LoadBMP can be written as

SDL_Surface *loadedSurface = SDL_LoadBMP(path);

The details of what std::string does and why .c_str() is needed are not relevant if you are not interested in learning C++, and in this case are not relevant to the tutorial either.

2

Nothing to do with the :: here. The std::string as a whole simply refers to a string, which might be expressed in C as a const char *(or char * maybe, for I guess C programmers do not care about the constness of variables). path.c_str() just means to get a const char * from the std::string variable path, so you can safely ignore it.

Ruifeng Xie
  • 813
  • 4
  • 16