2

I have seen a program, in the header file declared as below

typedef char CHAR10[10];

and in the program as below

CHAR10  szReading;

Is the above declaration same as

char szReading[10];

I am pretty much confused with these. Could someone explain what exactly it is?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Aravind
  • 149
  • 1
  • 2
  • 9

2 Answers2

1

Yes, your interpretation is correct.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • If i want to declare double array can i do `CHAR10 szReading[5]` is that correct? – Aravind Apr 27 '15 at 14:57
  • Yes. That would be equivalent to `char szReading[5][10]`. I'm not sure if that's what you mean by 'double array', though. IMHO it would be better to not hide important information with `typedef` in this manner. – Carl Norum Apr 27 '15 at 14:57
  • @Yes I meant to say 2D array, Thanks – Aravind Apr 27 '15 at 14:59
  • @Aravind: You need to be careful with the word "double". This: `double arr[5];` is a `double` array. – Keith Thompson Apr 27 '15 at 15:40
1

Yes, you understand it correctly. See this answer for more details.

When you say CHAR10 would be the same as an array of char with fixed length and then say CHAR10 test, then test will certainly be an array of char of the length you want (10 in your example).


Examples of typedef usage

typedef unsigned int uint;
uint K; // K is an unsigned int

typedef char *SortOfString;
SortOfString test; // test is a pointer to char
Community
  • 1
  • 1
ForceBru
  • 43,482
  • 10
  • 63
  • 98