mdh said:
> On May 11, 8:16 pm, Richard Heathfield <r...@[EMAIL PROTECTED]
> wrote:
>>
>> Writing code "to figure things out" is a two-edged sword. It can
>> certainly be helpful for discovering how things *appear* to work, but
it
>> is a poor guide for discovering how things *must* work.
>
>
> Point made and taken...thanks.
>
>
>> > char arr [3][3] = {"One", "Two", "Lst"};
>>
>> Legal, but dangerous. Don't treat these arrays of char as if they were
>> strings. They aren't.
>
> OK..now let me show my total ignorance.
>
> So, can multidim char arrays ever be treated as strings and would
> this then be the correct intialization?
Yes. There is *nothing special* about "multidim" arrays. For example:
char arr[6][15] = { "This", "string", "initialisation",
"works", "just", "fine" };
for(i = 0; i < 5; i++)
{
printf("%s\n", arr[i]);
}
> char arr[] [] = "one, two";
> And, for a regular (one dim)
All arrays are regular. A multidimensional array is simply a
one-dimensional array, each of whose elements happens to be an array.
Divide and conquer.
> char array, would this be a string
> intialization? char arr[]= "something"
Yes.
> or the use of strcpy?
No, strcpy can never initialise anything, because initialisation is what
you do to an object at the point where it is created. It says to the
compiler: "at the point where you are making an object of THIS type for
me, please give it THIS value". By the time you can call strcpy, it's too
late to initialise the array into which you are copying the string, but of
course it's not too late to update that array's members' values.
> And, somewhat philosophically, a (one dim) char array is simply a
> contiguous set of bytes. If one ? *knows* the array is NULL
> terminated,
A char array cannot be NULL terminated. C is a case sensitive language.
NULL is a null pointer constant. Strings are terminated by a null
character, not a NULL character (because there is no such thing as a NULL
character).
> then we can safely treat it as a string.
Right.
> But the *only*
> way to know this is if it is correctly initialized in the first place.
> Is this a fair way of looking at it?
Yes. When you create an object, put something in it, so that it has a
known
value. That way, later on when you examine that object, you know you're
/allowed/ to examine that object. It might not have the /right/ thing in
it (i.e. your program might have a bug) but at least it contains a
legitimate and reproducible value (i.e. debugging is made considerably
easier).
>> > p=&arr[i][j];
>> > printf( "%p\n", p);
>>
>> Although the representations of char * and void * are required to be
the
>> same, it is generally wiser to insist on void * for %p:
>>
>> printf("%p\n", (void *)p);
>
> I did read today about the need for a pointer to be cast to
> void ...but is there a reason for this?
Yes. The printf function specifies an interface that allows you to print a
void * value, but doesn't specify an interface for pointer values of other
types. If you would like a function that can print a pointer value of
*any* type, feel free to write one - but I should warn you that it will
take you infinitely long to write such a function in true generality.
> Other than the language says it should be so.
Isn't that a pretty powerful reason?
--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www.
+rjh@[EMAIL PROTECTED]
users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


|