Monday, April 6, 2009

Type categories

The four basic type categories (and their subcategories) are as follows:

Aggregate

Array
struct
union
class (C++ only)

Function
Scalar

Arithmetic
Enumeration
Pointer
Reference (C++ only)

void

Types can also be viewed in another way: they can be fundamental or derived types. The fundamental types are void, char, int, float, and double, together with short, long, signed, and unsigned variants of some of these. The derived types include pointers and references to other types, arrays of other types, function types, class types, structures, and unions.

A class object, for example, can hold a number of objects of different types together with functions for manipulating these objects, plus a mechanism to control access and inheritance from other classes

Given any nonvoid type type (with some provisos), you can declare derived types as follows
Declaring types

Declaration Description

type t; An object of type type
type array[10]; Ten types: array[0] - array[9]
type *ptr; ptr is a pointer to type
type &ref = t; ref is a reference to type (C++)
type func(void); func returns value of type type
void func1(type t); func1 takes a type type parameter
struct st {type t1; type t2}; structure st holds two types

Note: type& var, type &var, and type & var are all equivalent.


Void
Syntax

void identifier

Description

void is a special type indicating the absence of any value. Use the void keyword as a function return type if the function does not return a value.

void hello(char *name)

{
printf("Hello, %s.",name);

}

Use void as a function heading if the function does not take any parameters.

int init(void)

{
return 1;

}


{

return 1;

}





Void Pointers

Generic pointers can also be declared as void, meaning that they can point to any type.

void pointers cannot be dereferenced without explicit casting because the compiler cannot determine the size of the pointer object.

No comments:

Post a Comment