HP C A.06.05 Reference Manual
Program Organization
Declarations
Chapter 2 21
Using typedefs for Arrays
The following two examples illustrate what can happen when you mix pointers and typedefs
that represent arrays. The problem with the program on the left is that ptr points to an array
of 80 chars, rather than a single element of a char array. Because of scaling in pointer
arithmetic, the increment operator adds 80 bytes, not one byte, to ptr.
Name Spaces
All identifiers (names) in a program fall into one of four name spaces. Names in different
name spaces never interfere with each other. That is, you can use the same name for an object
in each of the four name spaces without these names affecting one another. Table 2-2 lists the
four name spaces:
Table 2-1 Mixing Pointers and Typedefs
Wrong Right
typedef char STR[80];
STR string, *ptr;
main()
{
ptr = string;
printf("ptr = %d\n", ptr);
ptr++;
printf("ptr = %d\n", ptr);
}
*** Run-Time Results ***
ptr = 3997696
ptr = 3997776
typedef char STR[80];
STR string;
char *ptr;
main()
{
ptr = string;
printf("ptr = %d\n", ptr);
ptr++;
printf("ptr = %d\n", ptr);
}
*** Run-Time Results ***
ptr = 3997696
ptr = 3997697
Table 2-2 Name Spaces
Name Spaces Description
Structure, Union,
and Enumeration
Tags
Tag names that immediately follow these type specifiers: struct,
union, and enum. These types are described in “Structure and
Union Specifiers” on page 48.
Member Names Names of members of a structure or union.