Simple C++ Questinon


Awakening

Active Member
Joined
Mar 5, 2009
Messages
666
Location
Sweden
Website
www.digitalawakening.se
I've been reading C++ tutorials and I got a question regarding Lazy Foo's SDL tutorials. In his 2nd lesson (and others) he sometimes writes 'SDL_Surface *variable' and sometimes 'SDL_Surface* variable'. Note the position of the *. Check the link below. I know that 'SDL_Surface *variable' declares a pointer so my question is if he have misplaced the * or if 'SDL_Surface* variable' have some other meaning?

CODE
http://www.lazyfoo.net/SDL_tutorials/lesson02/index.php
 
Last edited by a moderator:
It's just a preference.

"Type * name" is the same as "Type *name" is the same as "Type* name".

I usually use "Type * name" since it looks nice, but Qt uses "Type *name"...
There's a lot of little things that are just preferential, and so the language doesn't enforce them, and it causes endless argument amongst programmers.

Like spaces and tabs. Tabs are clearly superior, but some people have standardized on spaces, and there's 4-space indent and 8-space indent.
Where you put your brackets... ffs.
 
Last edited by a moderator:
Personal preference is all it is. Both are syntactically correct.
I put the pointer symbol with the type name as that makes more sense to me, as it is part of the type definition of the variable you are declaring.
 
I was guessing that the location of the * didn't matter (like some other characters) but since he was using 2 different ways to do it, and in more then 1 lesson, I was wondering why.

Thanks for replying :)

For me '*name' is the correct way because I think the * belong to the pointer, especially since you can use '*name' and 'name' later.
 
Last edited by a moderator:
I put the pointer symbol with the variable name, because of how multiple declarations work in C/C++:

int *x, y, z; // only x is a pointer, this is obvious
int* x, y, z; // same as above, but now it looks like they all should be
 
I use "Type* var;", because in my opinion the "Type*" is the type of the variable "var" and so they logically belong together. Then again, I never use multiple declaration so Exophase's reason is not relevant to me. I have been taught well to adhere to certain style rules :)

But yes, it's a matter of preference.
 
Last edited by a moderator:
'Exophase' said:
I put the pointer symbol with the variable name, because of how multiple declarations work in C/C++:

int *x, y, z; // only x is a pointer, this is obvious
int* x, y, z; // same as above, but now it looks like they all should be
+1
 
Last edited by a moderator:
'Exophase' said:
I put the pointer symbol with the variable name, because of how multiple declarations work in C/C++:

int *x, y, z; // only x is a pointer, this is obvious
int* x, y, z; // same as above, but now it looks like they all should be
exactly, from c/c++ parser viewpoint "*" is always associated to the right identifier, not the left identifier.

You can see that rule in :

typedef int *f(void) ==> f is the type of a function returning a pointer on an integer
typedef int (*f)(void) ==> f is the type of a pointer on a funtion returning an interger

people arguing about the associativity about * really need to read the c/c++ grammar like cparser.y (most time this "*" is a unary operator like "!")
 
Last edited by a moderator:
'hlide' said:
'Exophase' said:
I put the pointer symbol with the variable name, because of how multiple declarations work in C/C++:

int *x, y, z; // only x is a pointer, this is obvious
int* x, y, z; // same as above, but now it looks like they all should be
exactly, from parser viewpoint "*" is always associated to the right identifier, not the left identifier.

can you write "typedef T*;" ? of course not.

typedef int *f(void) ==> f is the type of a function returning a pointer on an integer
typedef int (*f)(void) ==> f is the type of a pointer on a funtion returning an interger


He wasn't talking practically, he was saying that it looks better.

Geez, actually read what he wrote.
 
Last edited by a moderator:
'Butterman' said:
He wasn`t talking practically, he was saying that it looks better.

Geez, actually read what he wrote.
i know it ! I`m just explaining why "int *x, y, z;" means only x is a pointer because of the way c/c++ parser works, that`s all.

note also that "typedef (int*) f(void);" generates an error, which is another reason why some people may prefer "int *x" instead of "int* x".

Of course, people are free to place it where they want to as long as it works.

EDIT: those """ are really a pain in the arse
 
Last edited by a moderator:
'dflemstr' said:
'hlide' said:
EDIT: those """ are really a pain in the arse
Read my sig.

I do use you script indeed :), but when re-editing a post, there is still some issues (that I forget to deal with). See your quote ;p.
 
Last edited by a moderator:
Hm, maybe I should fix that. If only Invision wouldn't use iframes fo posts (at least in enhanced mode). I'll look into it.

BTW, I don't see what's wrong with my quote since I have the script on :p
 
Last edited by a moderator:
Since we're talking about Lazy Foo's tutorials, I was wondering what this line meant:

Uint32 colorkey = SDL_MapRGB(optimizedImage -> format, 0, 0xFF, 0xFF);

Specifically, I don't understand what the -> means.
 
Last edited by a moderator:
'Ranma13' said:
Since we're talking about Lazy Foo's tutorials, I was wondering what this line meant:

Uint32 colorkey = SDL_MapRGB(optimizedImage -> format, 0, 0xFF, 0xFF);

Specifically, I don't understand what the -> means.
-> is used to access a field of a structure indirectly. Given A, which is a pointer to a structure X, then A->B refers to the field B in X. It's functionally equivalent to (*A).X
 
Last edited by a moderator:
'Ranma13' said:
Since we're talking about Lazy Foo's tutorials, I was wondering what this line meant:

Uint32 colorkey = SDL_MapRGB(optimizedImage -> format, 0, 0xFF, 0xFF);

Specifically, I don't understand what the -> means.
"->" is similar to "."

optimizedImage isn't an actual object, it's a pointer to an object. It's just the memory address of the actual image object.

Since it's a pointer, it doesn't make any sense to say, "optimizedImage.format" because optimizedImage is just a number.

"optimizedImage->format" says, "Go to this address, find the object there, AND THEN look for the format attribute.

It's equivalent to "(*optimizedImage).format", where you use the dereference symbol "*" to find the object itself, and then you get that object's format attribute.

So if you had something like "SDL_Surface optimizedImage", then you would use "optimizedImage.format", because you would be accessing the object itself.
 
Last edited by a moderator:
Ohh ok, I get it.

So Object -> Member is the same as *Object.Member then. I remember learning about this in class but I've only done Java programming since then, so I forgot about it. Thanks for the help!
 
Last edited by a moderator:
Watch out, those two are not the same thing at all. "." has greater precedence than "*", so you would say "take the structure Object, get it's member Member, and return the thing that Member is pointing to" which wouldn't make much sense since Object is a pointer to a structure.

EDIT: (*Object).Member is correct because this means "take the struct that Object is pointing to and retireve it's member Member". The () change precedence

BTW, in Java, you only have pointers to objects, never objects themselves. (Remember, all objects in Java are stored on the "heap", never on the "stack") so "->" in C++ is equivalent to "." in Java, yes.

@lulzfish Thats true in C++, but in C you dont have Objects, only structs :p
In this case it is a struct we're talking about. But I'm sure you knew that already.
 
Last edited by a moderator:
I see your point, dflemstr.

I'm interested in doing some Pandora developing but I want to check out the device first before I start anything. I've currently done some Java and C# programming, but since both are reference-based, I'm a bit rusty when it comes to explicitly separating objects and pointers.
 
Last edited by a moderator:
Back
Top