Some more on Strings, and Arrays of Strings
Thursday, February 21st, 2008Some more on Strings, and Arrays of Strings Well, let’s go back to strings for a bit. In the following
all assignments are to be understood as being global, i.e. made
outside of any function, including main.
We pointed out in an earlier chapter that we could write:
char my_string[40] = “Ted”;
which would allocate space for a 40 byte array and put the string
in the first 4 bytes (three for the characters in the quotes and
a 4th to handle the terminating ‘\0′.
Actually, if all we wanted to do was store the name “Ted” we
could write:
char my_name[] = “Ted”;
and the compiler would count the characters, leave room for the
nul character and store the total of the four characters in memory
the location of which would be returned by the array name, in this
case my_string.
In some code, instead of the above, you might see:
char *my_name = “Ted”;
which is an alternate approach. Is there a difference between
these? The answer is.. yes. Using the array notation 4 bytes of
storage in the static memory block are taken up, one for each
character and one for the terminating nul character. But, in the
pointer notation the same 4 bytes required, _plus_ N bytes to
store the pointer variable my_name (where N depends on the system
but is usually a minimum of 2 bytes and can be 4 or more).
In the array notation, “my_name” is short for &myname[0]
which is the address of the first element of the array. Since
the location of the array is fixed during run time, this is a
constant (not a variable). In the pointer notation my_name is a
variable. As to which is the _better_ method, that depends on
what you are going to do within the rest of the program.
Let’s now go one step further and consider what happens if
each of these declarations are done within a function as opposed
to globally outside the bounds of any function.
void my_function_A(char *ptr)
{
char a[] = “ABCDE”;
.
.
}
void my_function_B(char *ptr)
{
char *cp = “ABCDE”;
.
.
}
Here we are dealing with automatic variables in both cases.
In my_function_A the automatic variable is the character array
a[]. In my_function_B it is the pointer cp. While C is designed
in such a way that a stack is not required on those systems
which don’t use them, my particular processor (80286) and
compiler (TC++) combination uses a stack. I wrote a simple
program incorporating functions similar to those above and found
that in my_function_A the 5 characters in the string were all
stored on the stack. On the other hand, in my_function_B, the 5
characters were stored in the data space and the pointer was
stored on the stack.
By making a[] static I could force the compiler to place the
5 characters in the data space as opposed to the stack. I did
this exercise to point out just one more difference between
dealing with arrays and dealing with pointers. By the way, array
initialization of automatic variables as I have done in
my_function_A was illegal in the older K&R C and only “came of
age” in the newer ANSI C. A fact that may be important when one
is considering portability and backwards compatibility.
As long as we are discussing the relationship/differences
between pointers and arrays, let’s move on to multi-dimensional
arrays. Consider, for example the array:
char multi[5][10];
Just what does this mean? Well, let’s consider it in the
following light.
char multi[5][10];
^^^^^^^^
Let’s take the underlined part to be the “name” of an array.
Then prepending the “char” and appending the [10] we have an
array of 10 characters. But, the name “multi[5]” is itself an
array indicating that there are 5 elements each being an array of
10 characters. Hence we have an array of 5 arrays of 10
characters each..
























