Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: Two dimensi...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 26 of 32 Topic 26121 of 26977
Post > Topic >>

Re: Two dimensional array Question

by Richard Heathfield <rjh@[EMAIL PROTECTED] > May 13, 2008 at 04:48 AM

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
 




 32 Posts in Topic:
Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-08 08:16:13 
Re: Two dimensional array Question
Joe Wright <joewwright  2008-05-08 19:15:37 
Re: Two dimensional array Question
"Default User"   2008-05-08 23:24:55 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-08 21:03:36 
Re: Two dimensional array Question
"Default User"   2008-05-09 16:27:53 
Re: Two dimensional array Question
Ben Bacarisse <ben.use  2008-05-09 00:30:00 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-08 18:39:27 
Re: Two dimensional array Question
Barry Schwarz <schwarz  2008-05-08 20:54:09 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-09 06:18:51 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-09 20:35:22 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-10 07:09:06 
Re: Two dimensional array Question
Ben Bacarisse <ben.use  2008-05-10 12:42:22 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-10 08:43:58 
Re: Two dimensional array Question
Joe Wright <joewwright  2008-05-10 09:26:20 
Re: Two dimensional array Question
Ben Bacarisse <ben.use  2008-05-10 14:53:55 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-10 12:25:49 
Re: Two dimensional array Question
Ben Bacarisse <ben.use  2008-05-10 17:12:46 
Re: Two dimensional array Question
Ben Bacarisse <ben.use  2008-05-11 01:17:26 
Re: Two dimensional array Question
Barry Schwarz <schwarz  2008-05-11 04:17:57 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-11 19:07:45 
Re: Two dimensional array Question
Richard Heathfield <rj  2008-05-12 03:16:04 
Re: Two dimensional array Question
Richard Heathfield <rj  2008-05-12 03:17:11 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-12 07:18:50 
Re: Two dimensional array Question
"Default User"   2008-05-12 06:19:46 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-12 21:12:17 
Re: Two dimensional array Question
Richard Heathfield <rj  2008-05-13 04:48:22 
Re: Two dimensional array Question
Chris Torek <nospam@[E  2008-05-13 16:38:01 
Re: Two dimensional array Question
Richard Heathfield <rj  2008-05-13 16:53:43 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-12 21:13:55 
Re: Two dimensional array Question
pete <pfiland@[EMAIL P  2008-05-16 22:17:14 
Re: Two dimensional array Question
mdh <mdeh@[EMAIL PROTE  2008-05-12 22:26:08 
Re: Two dimensional array Question
Barry Schwarz <schwarz  2008-05-16 18:44:29 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Jul 26 3:50:06 CDT 2008.