From: ratheesh k <ratheesh.ksz@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: linux C array simplified.
Date: Wed, 12 Jan 2011 21:10:30 +0530 [thread overview]
Message-ID: <AANLkTintj70bSB+2MuR21EydOJX9cXpFiDQ4JMy+SSKa@mail.gmail.com> (raw)
I wrote program to make c array simple. I have added comments in
between so that my student could understand it easily.
Could you please check whether this comments are correct. Could you
correct me if i am wrong. Thanks in advance.
********************************************************************************************************************************************************
# include <stdio.h>
int functionA(int (*ptr)[10])
{
/* passing array address. */
/* ptr pointer to an array and &ptr is the pointer to array pointer
Both are of size 4 bytes */
printf("sizeof(ptr) = %ld\n", sizeof(ptr));
printf("sizeof(&ptr) = %ld\n", sizeof(&ptr));
/* if ptr is incremented . ptr + 10*4 will be the answer */
printf("pointer address ptr=%p ptr+1=%p\n" , ptr ,ptr+1);
/*since ptr is an array pointer ; element are accessed like (*ptr[0])
*/
printf("access memory ptr=%d\n" , (*ptr)[0] );
/* we could do this by *(*ptr)+0); this is
same as **ptr
*/
printf("access memory **ptr=%d\n" , (**ptr) );
/* access memory at *(*ptr+1)) */
printf("access memory *(*ptr+1)=%d\n" , *(*ptr+1) );
return 0;
}
int functionB(int *abc )
{
/* address of first element is passed */
/* sizof pointer is 4 bytes */
printf("sizeof(abc) = %ld\n" ,sizeof(abc));
/* pointer increment will point to next element. pointes is of type (int *)
*/
printf("pointer address abc=%p abc+1=%p\n", abc ,abc+1 );
/* we could access memory locations by dereferencing each location */
printf("access memory at *(abc)=%d, *(abc+1)=%d\n" , *abc ,*(abc+1));
}
int functionC(int pqr[])
{
/* prq[] is same as char *abc (in functionA ). eventhough we have
pqr[] declaration
compiler will treat this as simple char *pqr.
Please refer funnctiuonA for all explanations
*/
printf("sizeof(pqr) = %ld\n" ,sizeof(pqr));
printf("pointer address pqr=%p pqr+1=%p\n", pqr ,pqr+1 );
printf("access memory at *(pqr)=%d, *(pqr+1)=%d\n" , *pqr ,*(pqr+1));
}
int functionD(int stp[20])
{
/*stp[20] is also treated as simple char *stp */
printf("sizeof(stp) = %ld\n" ,sizeof(stp));
printf("pointer address stp=%p stp+1=%p\n", stp ,stp+1 );
printf("access memory at *(stp)=%d, *(stp+1)=%d\n" , *stp ,*(stp+1));
}
int main()
{
/* integer array is defined and values are initialized */
int arr[10]={1,2,3,4,5,6,7,8,9,0};
/* sizeof(arr) gives totoal sizeof array. that is equal to
no_of_elements * sizeof_an_element
here it 10*4 = 40
*/
printf("Sizeof(arr) = %ld\n", sizeof(arr));
/* address of array is &arr and its size is of 32bit (4bytes) */
printf("sizeof(&arr)=%ld\n" , sizeof(&arr));
/* sizeof(int) is 4 and arr[0] contains and integer */
printf("sizeof(arr[0])=%ld\n" , sizeof(arr[0]));
/* arr contains pointer to first element of the array . so it is
incremented
it will point to next element
*/
printf("pointer address arr=%p arr+1=%p \n" ,arr, arr+1 );
/* &arr contains array address. so if it is incremented, it will
point to (&arr) + (no.of.elements) * (sizof.each.element)
eg: if &arr is 1000 ;then
1000 + 10*4 =1040
*/
printf("pointer address &arr=%p &arr+1=%p\n" ,&arr ,&arr+1);
/* arr points to first element of the array. and *arr dereference
first element */
printf("access element *arr=%d\n", *arr);
/* normal array access */
printf("access element arr[0]=%d\n", arr[0]);
/* (arr+1) points to next element in the array */
printf("access element *(arr+1)=%d\n", *(arr+1));
functionA(&arr);
functionB(arr);
functionC(arr);
functionD(arr);
}
next reply other threads:[~2011-01-12 15:40 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-01-12 15:40 ratheesh k [this message]
2011-01-12 16:39 ` linux C array simplified Michal Nazarewicz
2011-01-14 0:09 ` Andrej Gelenberg
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=AANLkTintj70bSB+2MuR21EydOJX9cXpFiDQ4JMy+SSKa@mail.gmail.com \
--to=ratheesh.ksz@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).