|Access||Adobe photoshop||Algoritmi||Autocad||Baze de date||C||C sharp|
|Calculatoare||Corel draw||Dot net||Excel||Fox pro||Frontpage||Hardware|
|Php||Power point||Retele calculatoare||Sql||Tutorials||Webdesign||Windows|
|Asp||Autocad||C||Dot net||Excel||Fox pro||Html||Java|
returns the size in bytes of its operand. Whether the result of
unsigned int or
long is implementation defined—which is why the declaration of
malloc above ducked the issue by omitting any parameter information; normally
you would use the
header file to declare
correctly. Here is the last example done portably:
The operand of
only has to be parenthesized if it's a type name, as it was in the example. If
you are using the name of a data object instead, then the parentheses can be
omitted, but they rarely are.
In the last example, the array
is an array of
after the call to
(assuming that it was successful),
will point to a region of store that can also be treated as an array of
The fundamental unit of storage in C is the
char, and by definition
is equal to 1, so you could allocate space for an
array of ten
while to allocate room for an array of ten
ints, you would have to use
malloc can't find
enough free space to satisfy a request it returns a null pointer to indicate
failure. For historical reasons, the
header file contains a defined constant called NULL which is traditionally used
to check the return value from
and some other library functions. An explicit
(void *)0 could equally well be used.
As a first illustration of the use of
here's a program which reads up to
strings from its input and sort them into alphabetical order using the library
strcmp routine. The strings are
terminated by a ‘
character. The sort is done by keeping an array of pointers to the strings and
simply exchanging the pointers until the order is correct. This saves having to
copy the strings themselves, which improves the efficency somewhat.
The example is done first using fixed size arrays then another version uses malloc and allocates space for the strings at run time. Unfortunately, the array of pointers is still fixed in size: a better solution would use a linked list or similar data structure to store the pointers and would have no fixed arrays at all. At the moment, we haven't seen how to do that.
The overall structure is this:while(number of strings read < MAXSTRING
A number of functions are used to implement this program:
Read a line of characters terminated by ‘
n’ from the program's input. The first
MAXLEN-1 characters are written into the
array pointed to by destination.
If the first character read is
EOF, return a null pointer, otherwise
return the address of the start of the string (destination). On return,
destination always points to a null-terminated string.
sort_arr(const char *p_array)
P_array is an array of pointers to characters.
The array can be arbitrarily long; its end is indicated by the first element
containing a null pointer.
sorts the pointers so that the pointers point to strings which are in
alphabetical order when the array is traversed in index order.
print_arr(const char *p_array)
sort_arr, but prints the strings in index order.
It will help to understand the examples if you remember that in an
expression, an array's name is converted to the address of its first element.
Similarly, for a two-dimensional array (such as
strings below), then the expression
strings has type char, but
strings has type ‘array of
char’ which is therefore converted to
the address of the first element: it is equivalent to
It is no accident that
returns a pointer. We can now dispense with the strings array by getting
next_string to allocate its own storage.
Finally, for the extremely brave, here is the whole thing with even
p_array allocated using
malloc. Further, most of the array
indexing is rewritten to use pointer notation. If you are feeling queasy, skip
this example. It is hard. One word of explanation:
char **p means a pointer to a pointer to
a character. Many C programmers find this hard to deal with.
To further illustrate the use of
another example program follows which can cope with arbitrarily long strings.
It simply reads strings from its standard input, looking for a newline
character to mark the end of the string, then prints
the string on its standard output. It stops when it detects end-of-file. The
characters are put into an array, the end of the string being indicated (as
always) by a zero. The newline is not stored, but used to detect when a full
line of input should be printed on the output. The program doesn't know how
long the string will be, so it starts by allocating ten characters—enough for a
If the string is more than ten characters long,
malloc is called to allocate room for
the current string plus ten more characters. The current characters are copied
into the new space, the old storage previously allocated is released and the
program continues using the new storage.
To release storage allocated by
the library function
used. If you don't release storage when it isn't needed any more, it just hangs
around taking up space. Using
allows it to be ‘given away’, or at least re-used later.
The program reports errors by using
a close cousin of
The only difference between them is that fprintf takes an additional first
argument which indicates where its output should go. There are two constants of
the right type for this purpose defined in
stdout indicates that
the program's standard output is to be used;
refers to the program's standard error stream. On some systems both may be the
same, but other systems do make the distinction.
That may not be a particularly realistic example of how to handle
arbitrarily long strings—for one thing, the maximum storage demand is twice
the amount needed for the longest string—but it does actually work. It also
costs rather a lot in terms of copying around. Both problems could be reduced
by using the library
A more sophisticated method might use a linked list, implemented with the use of structures, as described in the next chapter. That would have its drawbacks too though, because then the standard library routines wouldn't work for a different method of storing strings.
One common mistake made by beginners is shown below:#include <stdio.h>
The numbers printed will not be the same. The first will,
correctly, identify the size of
const char, which can only have a size of
arr is different: array of
const char. The confusion arises because
this is the one place that the use of an array is not converted into a pointer
first. It is never possible, using
to find out how long an array a pointer points to; you must have a
genuine array name instead.
Now comes the question of just what this does:sizeof ( sizeof (anything legal) )
That is to say, what type does the result of
sizeof have? The answer is that it is implementation
defined, and will be either
unsigned int, depending
on your implementation. There are two safe things to do: either always cast the
return value to unsigned long, as the examples have done, or to use the defined
size_t provided in the
<stddef.h> header file. For
Politica de confidentialitate|
Adauga cod HTML in site