Declaring Multiple Object Pointers on One Line Causes Compiler Error

Declaring multiple object pointers on one line causes compiler error

sf::Sprite* re_sprite_hair, re_sprite_body, re_sprite_eyes;

Does not declare 3 pointers - it is one pointer and 2 objects.

sf::Sprite* unfortunately does not apply to all the variables declared following it, just the first. It is equivalent to

sf::Sprite* re_sprite_hair;
sf::Sprite re_sprite_body;
sf::Sprite re_sprite_eyes;

You want to do:

sf::Sprite *re_sprite_hair, *re_sprite_body, *re_sprite_eyes;

You need to put one star for each variable. In such cases I prefer to keep the star on the variable's side, rather than the type, to make exactly this situation clear.

How are Pointers, data type initialization and multiple pointer declaration handled in c++?

In std::queue<TreeNode*> q;, the brackets enclose a complete type name. (By “name,” I mean the entire declarator for the type, TreeNode *, not an identifier.) When describing a type with a declarator, the form used is as if a complete normal declaration with an identifier had its identifier removed. For example, starting with a declaration TreeNode *Placeholder, the type name is TreeNode *.

In TreeNode* l = nullptr, r = nullptr;, there are no brackets to group the * with TreeNode. According to the C++ grammar, it is grouped with l. If brackets were allowed here, so that we could write <TypeNode *> l = nullptr, r = nullptr;, then that could work. The issue here is the same as if you write - a + b: The rules are simply that - is grouped with a, so the expression is -a + b, not - (a + b).

You can of course work around this with typedef TreeNode *TreeNodeP; TreeNodeP l = nullptr, r = nullptr;. This shows the effects are simply the results of how the C++ grammar is structured—there is no inherent reason a compiler could not accept any type description first and then a list of identifiers to be declared with that type, but the grammar design simply does not support that.

(One consequence of this is that it is misleading to write int* a, because the proximity of tokens in the text is different from their proximity in the grammar—* is closer to int in the text but is closer to a in the grammar, so the spacing sends the wrong message.)

C++ for loop with multiple pointer initializers

If you don't want to repeat the * then you could use using and create an alias BlockPtr which you use instead of Block*:

int main() {
using BlockPtr = Block*;

BlockPtr aList = new Block(1);
BlockPtr bList = new Block(2);

for (BlockPtr a = aList, b = bList; a != nullptr; a = a->next, b = b->next)
if (aList->data != bList->data)
cout << "Difference found.\n";
}

Or relay on auto:

int main() {

auto aList = new Block(1);
auto bList = new Block(2);

for (auto a = aList, b = bList; a != nullptr; a = a->next, b = b->next)
if (aList->data != bList->data)
cout << "Difference found.\n";
}

C multiple single line declarations

Only x is a pointer to int; y and z are regular ints.

This is one aspect of C declaration syntax that trips some people up. C uses the concept of a declarator, which introduces the name of the thing being declared along with additional type information not provided by the type specifier. In the declaration

int* x, y, z;

the declarators are *x, y, and z (it's an accident of C syntax that you can write either int* x or int *x, and this question is one of several reasons why I recommend using the second style). The int-ness of x, y, and z is specified by the type specifier int, while the pointer-ness of x is specified by the declarator *x (IOW, the expression *x has type int).

If you want all three objects to be pointers, you have two choices. You can either declare them as pointers explicitly:

int *x, *y, *z;

or you can create a typedef for an int pointer:

typedef int *iptr;
iptr x, y, z;

Just remember that when declaring a pointer, the * is part of the variable name, not the type.

Can't allocate a pointer to object of abstract type?

The problem is that this syntax:

gkAnimation* run = NULL, walk = NULL, idle = NULL;

Does not mean:

gkAnimation* run = NULL;
gkAnimation* walk = NULL;
gkAnimation* idle = NULL;

It means:

gkAnimation* run = NULL;
gkAnimation walk = NULL; /* invalid */
gkAnimation idle = NULL; /* invalid */

You need to explicitly define each item in the list as a pointer:

gkAnimation *run = NULL, *walk = NULL, *idle = NULL;

This is why many prefer the syntax style of placing the pointer next to the variable rather than next to the type.

Using multiple file pointers trying to open select files at a time in C..?

The definition are wrong, it defines fpBP1 as a pointer to FILE, and all other 4 are objects of type FILE.

FILE* fpBP1, fpBP2, fpBP3, fpBP4, fpBP5;

You should use the following:

FILE *fpBP1, *fpBP2, *fpBP3, *fpBP4, *fpBP5;

Or better, you should just do:

FILE *fpBP1;
FILE *fpBP2;
FILE *fpBP3;
FILE *fpBP4;
FILE *fpBP5;


Related Topics



Leave a reply



Submit