Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Pointer Declaration With typedef

In C++, if we want to declare multiple pointers, we would do something like this: int *a, *b, *c; where we would have to put an asterisk * in front of each of them. if i write this code: typedef int* ptr; ptr a,b,c;? Will they all be pointers, or just a?

like image 968
user2653125 Avatar asked Sep 10 '13 14:09

user2653125


1 Answers

No, typedef isn't just a matter of text substitution (like a macro would be).

typedef int* ptr;

introduces a new name, "ptr", for the type int*.

If you write

ptr a, b, c;

all of a, b, and c will have the same type, int*.

Note that

const ptr p;

likewise isn't the same as

const int* p;

Since ptr is a pointer type, the const applies to the pointer; the equivalent is

int* const p;
like image 95
molbdnilo Avatar answered Oct 25 '22 00:10

molbdnilo