5

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?

10
  • Any reason you couldn't use std::unique_ptr and then just typedef that? Commented Sep 10, 2013 at 14:49
  • @Mgetz The reason being this is the first i time i have heard of unique_ptr.. Sorry, i am still i noob in C++. Thank you i will check that later. Commented Sep 10, 2013 at 14:52
  • 4
    Any reason why you have to define more than one variable on the line. It makes the code more difficult to read. (As does this sort of typedef.) Commented Sep 10, 2013 at 14:52
  • 4
    @Mgetz Any reason why he should? He's got pointers to int. There's practically never any reason to dynamically allocate int, so presumably, they don't point to dynamically allocated memory. Commented Sep 10, 2013 at 14:54
  • 2
    This is a perfectly good question about basic C++ syntax, which is completely unrelated to the pros and cons of different memory management methods. Commented Sep 10, 2013 at 15:08

4 Answers 4

5

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;
Sign up to request clarification or add additional context in comments.

1 Comment

that's one reason to using typedef instead of Macro.
3

Hmmm, interesting... Lets see. Say I don't know many things about typedefs, what do I do?


Answer: I test it by myself. Below there is a code which answers your question:

#include <stdio.h>

typedef int* pointer_t;
pointer_t a1, b1, c1;
int* a2, b2, c2;

int main() {
  a1 = new int; // ok
  b1 = new int; // ok
  c1 = new int; // ok
  a2 = new int; // ok
  b2 = new int; // compile-time error
  c2 = new int; // compile-time error

  delete a1; // ok
  delete b1; // ok
  delete c1; // ok
  delete a2; // ok
  delete b2; // compile-time error
  delete c2; // compile-time error

  return 0;
}

Conclusion: Both a1, b1, and c1 will be pointers, but only a2 will be a pointer.

2 Comments

For b2 and c2 to be pointers, you'll need to declare them int *a2, *b2, *c2. As said by others, pointer_t will be of type int*, so a1, b1 and c1 will be int *. Splitting your declarations one in each line you have: pointer_t a1; pointer_t b1; pointer_t c1; int* a2; int b2, int c2;
Thank you Captain Obvious. but by asking it in stackoverflow i learn more than just understanding how it works, but also why, and some other suggestions like the answers above. Still i agree, you made a good point. :)
1

Yes, they will be pointers:

typedef int* pointer_to_int;

int main()
{
    int a , b , c;
    pointer_to_int ptr_a , ptr_b , ptr_c;

    ptr_a = &a;
    ptr_b = &b;
    ptr_c = &c;

    *ptr_a = 1;
    *ptr_b = 2;
    *ptr_c = 3;

    std::cout << a << " " << b << " " << c;
}

The output is:

1 2 3

Thats because typedef defines type aliases. On the other hand, if you could use C++11, I recommend you to use using aliases instead of typedef, because its syntax is much clear:

using ptr_to_int = int*;

and also works with template aliases:

template<typename T>
using vector_t = std::vector<T>;

Comments

0

They will all be a pointers, the typedef is int*.

1 Comment

History will forget this u-turn :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.