Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 748 Vote(s) - 3.53 Average
  • 1
  • 2
  • 3
  • 4
  • 5
typedef array type in C

#1
`typedef int arr[10]`

I understand that the above statement defines a new name for int[10]
so that

arr intArray;

is equivalent to

int intArray[10];

However, I am confused by the convention for doing this. It seems to me that

`typedef int arr[10]`

is confusing and a clear way to me is

`typedef int[10] arr`

i.e. I define the "int[10]" to be a new type called arr

However, the compiler does not accept this.

May I ask why ? Is it just a convention for C language ?
Reply

#2
Because that's how the language was defined. My guess is that, in part, it is so that what goes into a `typedef` follows the same rules as a normal variable declaration, except that the name is the name of the type instead of a variable (and thus that part of the parser can be re-used).
Reply

#3
Very early versions of C didn't have the `typedef` keyword. When it was added to the language (some time before 1978), it had to done in a way that was consistent with the existing grammar.

Syntactically, the keyword `typedef` is treated as a *storage class specifier*, like `extern` or `static`, even though its meaning is quite different.

So just as you might write:

static int arr[10];

you'd write:

typedef int arr[10];

Think of a `typedef` declaration as something similar to an object declaration. The difference is that the identifier it creates (`arr` in this case) is a type name rather than an object name.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through