Hi,
I'm attempting to understand the use of pointers(at least grasp how
pointers work). I've read the FAQ on
http://www.eskimo.com/~scs/C-faq/s6.html on pointers and arrays but I'm
still a bit lost.
I written the following code to try to understand it but it's not
working:
#include <stdio.h>
#include <ctype.h>
#define MAXROW 2
#define MAXCOL 5
void init_array(char *data[MAXROW]);
void print_array(char *data[MAXROW]);
int main ()
{
char *array_ptr[MAXROW];
array_ptr is an array of MAXROW pointers to char. If we assume for
discussion that a pointer to char is 4 bytes, then you have a block of
memory MAXROW*4 bytes long reserved for this array. However, these
bytes are not initialized. Their value is indeterminate.
Colloquially, none of the pointers actually point anywhere yet because
they have not been initialized.
Normally, evaluating an initialized object would cause undefined
behavior. However, an expression of type array has the special
property that in most contexts, including this one, evaluating the
expression produces a value equal to the address of the first element
with type pointer to element type. So the use of array_ptr here is
well defined and exactly equivalent to &array_ptr[0].
print_array(array_ptr);
return 0;
}
void init_array(char *data[MAXROW])
{
int row, col;
for (row=0; row<MAXROW; row++)
{
for (col=0; col<MAXCOL; col++)
{
(data[row] + col) = '\0';
Here is where you get into trouble. Consider the iteration when row
is 0.
data[0] is the first pointer in array_ptr from main. You never
initialized this pointer to point anywhere. Attempting to evaluate an
indeterminate value invokes undefined behavior.
Consider the code segment
int i;
i = i + 1;
Because i is not initialized, there is no previous value to add 1 to
and this invokes the same undefined behavior. However, with either
int i = 5;
i = i + 1;
or
int i;
i = 5;
...
i = i + 1;
there is a previous value of i to add 1 to and both result in i having
the value 6.
The same situation is true for you pointer. In order for this
expression to evaluate properly, you must first assign it a value. In
this case, that value must be the address of a char. Since you intend
to use this pointer to reference a row of the matrix, it needs to
point to the first of a sequence MAXCOL char. There are two common
approaches for this:
One is to allocate space dynamically:
data[0] = malloc(MAXCOL * *data[0]);
The other is to define an array of char and use the address of
the array:
char row0[MAXCOL];
...
data[0] = row0;
Now that data[0] evaluates properly, we address the rest of the
expression. On the first iteration, col is also 0. data[0] has type
pointer to char and 0 has type int so this is an example of pointer
arithmetic. The expression evaluates to the address 0 bytes past the
char that data[0] points to and has type pointer to char.
So your are attempting to assign the character '\0' (which is
really an int) to an l-value that has type pointer. Normally,
attempting to assign an int to a pointer is a syntax error and would
result in a diagnostic about incompatible types. However, any integer
expression that evaluates to 0, as '\0' does, is recognized as a valid
NULL pointer constant which can be assigned to a pointer to char.
So while you don't have that syntax error, this would not do what
you want. The error message you receive is telling you that the
expression on the left of your assignment statement is not acceptable
as the recipient of a value. You want to assign the character '\0' to
the char that your pointer expression points to. To do this, you need
to dereference the pointer because you want to manipulate the object
it points to.
The dereference operator (*) would work
*(data[row] + col) = '\0';
but the normal C idiom for this is
data[row][col] = '\0';
}
}
}
void print_array(char *data[MAXROW])
{
int row, col;
for (row=0; row<MAXROW; row++)
{
for (col=0; col<MAXCOL; col++)
{
printf("%c",*(data[row] + col));
You have used the dereference operator properly here.
If you correct the problems with array_ptr not being initialized
properly, you will still have a problem printing this way. Your
entire matrix is filled with '\0'. This is normally not a printable
or displayable character. Attempting to print it with %c will prove
very unsatisfactory.
You could print it with %d (and get 0) or you could choose to
initialize the matrix with printable characters.
You probably want this up one line so it executes at the end of every
row and not just at the end of the matrix.
}
When I try to compile it with gcc, there is a warning
a3.c: In function `init_array':
a3.c:29: invalid lvalue in assignment
(data[row]+col) does not define an object that can receive a value.
Can anyone tell me what the warning means ?
What I am doing wrong ?
Nat
<<Remove the del for email>>