Coder's Guild Mailing List

Re: array & structure...

Posted by Mark R Burford on 2001-04-10

Writing a tutorial  on pointers and arrays....

That would be a good idea as from my experience they dont seem to teach the
'behind the scenes' aspect of programming too much at my University.

If you could stick it on the web some place and I will inform my friends.


----- Original Message -----
From: Sharvil Nanavati <snrrrub@xxxxx.xxx>
To: <mrb199@xxx.xxxxx.xx.xx>
Sent: Tuesday, April 10, 2001 2:37 PM
Subject: Re: array & structure...


> > hi friends...
> >  kin ani body tell me...
> >
> >  in C v kin write :
> >
> >  struct test s1,s2;
> > /* initialize s1*/
> >
> > and then: s2 = s1;
> >
> >
> > but v kan't write :
> >
> > char array1[10], array2[10];
> > /* initialize array1*/
> >
> > and then : array1 = array2;
> >
> >
> > why??????
>
> As I had mentioned before, an array is like a pointer. Actually, an
> array is just a pointer that has a constant value and that has already
> allocated memory for itself. Therefore, the variable 'array1' POINTS TO
> a memory location, as does the variable 'array2'. Unlike pointers, you
> CANNOT change the memory location an array points to. Hence,
> array1=array2 does not work because you are REALLY saying is: "Let
> array1 point to the memory location that array2 points to."
>
> What is happening in the case of the 2 structures is that their VALUES
> are being copied. The variable 'array1' is a pointer, but it is
> dereferenced by using array notation: array1[0]. Thus, array1[0] is the
> value stored at the first memory location pointed to by 'array1'. If
> you want to copy one array to another, this would work:
>
> char array1[10], array2[10];
> int i;
>
> for(i = 0; i < 10; i++)
>     array1[i] = array2[i];
>
> A more direct approach is to do something like this:
>
> memcpy(array1, array2, sizeof(array1));
>
> I prefer the latter. Do use this with caution, however. If array1 was
> declared as a (true) pointer, the sizeof(...) function would give the
> size of the pointer, not the amount of memory allocated.
>
> I have noticed that many people are having difficulties with
> pointers/arrays. Would it be beneficial if I wrote a tutorial on this
> topic? If anyone has any comments about this, do write back.
>
> Cheers,
> Sharvil Nanavati
>
> __________________________________________________
> Do You Yahoo!?
> Get email at your own domain with Yahoo! Mail.
> http://personal.mail.yahoo.com/
> >