2

I define a typedef

typedef char* charP;

Then I declare a few variables

charP dog, cat, fish;

Are all the variables of type char* or is dog the only char* while cat and fish are of type char?

4
  • 1
    All of them are char * Commented Jul 7, 2014 at 11:20
  • 2
    printf("%d %d %d\n", sizeof dog, sizeof cat, sizeof fish); would have given you the answer in an instant. Commented Jul 7, 2014 at 11:23
  • 2
    @glglgl The proper formatting code for values of type size_t is %zu. Commented Jul 7, 2014 at 11:30
  • 1
    @unwind While you are completely right, for a quick test it should work as well. But there is a chance that I run into trouble with that, so better do it right your way. But then I'd have to lookup the z, and I hadn't the answer in an instant, but in several minutes :-} Commented Jul 7, 2014 at 11:33

3 Answers 3

10

All of them are of type charP, which is an alias for char *, so yes, they're all pointers.

That said, some people (me included) consider it a bad idea to "hide" the pointer asterisk, since it breaks the symmetry between declaring the variable and accessing it.

You're going to have:

charP a;

*a = '1';  /* What?! It didn't look like a pointer, above?! */

... which causes confusion. Generally, pointers in C are important to keep track of, so hiding what is a pointer and what isn't can lead to trouble.

Sign up to request clarification or add additional context in comments.

2 Comments

@Quentin Yeah, I don't quite ... agree that those were ever a good idea. :)
Well, MS loves the Macros and typedefs, some of them even make sense because they hint at semantics (like BSTR). Would be nice if they changed to standard types where possible someday, though...
1

All of them are char *. Do not confuse it with this case: char *dog, cat, fish;. Here, dog is a char *, and rest are just chars.

Comments

0

All of them are char pointers as you are using typedef and not macro.

so it will not behave like char *dog, cat, fish;

Comments

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.