• Welcome to Jose's Read Only Forum 2023.
 

ProgEx03

Started by Frederick J. Harris, November 04, 2009, 04:01:58 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Frederick J. Harris


/*
 ProgEx03.c

 In C programming the biggest and most important topic is pointers.  A pointer
 is a variable that holds the address of another variable.  In the context of
 variable declarations, the '*' symbol is used to declare pointers.

 On 32 bit Windows operating systems a signed or unsigned integer is 32 bits or
 four bytes.  In the program below the array iNums[] is declared and initialized
 with 6 integers so the size of the array could be determined with C's sizeof
 function to be 6 * 4 or 24 bytes...

 sizeof(iNums)=24

 However, the size of any of the individual integers is just 4 bytes...

 sizeof(iNums[0])=4,    sizeof(iNums[1]=4, etc.

 So, in a loop which will iterate through the values the loop must be executed...

 sizeof(iNums) / sizeof(iNums[0]  =  6  times

 in other words...

 for(i=0; i<6; i++)       // this                    For i=0 To 5
 {                        //    is                     ....
     ....                 //       C                   ....
     ....                 //         notation          ....
 }                        //           for           Next i

 In C one can represent...

 i = i + 1

 as

 i++;

 The important point to grasp in the program below is that the name of an array
 without the brackets is a pointer to the first element or the beginning of the
 array.  The way I think about an expression such as...

 *pNum or *iNums

 is...

 ...'what's stored at pNums' or 'what's stored at iNums'.  You have to be able
 to make a distinction in your mind between the address of a variable, and what
 is stored at that address.
*/

#include <stdio.h>

int main(void)
{
unsigned int iNums[]={2,4,6,8,10,12};       //array of six unsigned integers
unsigned int *pNum=NULL;                    //pointer to an unsigned integer
register unsigned int i;                    //unsigned integer loop variable

puts("i\t&iNums[i]\tiNums[i]");
puts("================================");
for(i=0;i<sizeof(iNums)/sizeof(iNums[0]);i++)
     printf("%u\t%u\t\t%u\n",i,(unsigned int)&iNums[i],iNums[i]);
puts("\n\ni\tpNum\t\t*pNum");
puts("================================");
pNum=iNums;   //Initialize pointer variable with start of array iNums
for(i=0;i<sizeof(iNums)/sizeof(iNums[0]);i++)
{
     printf("%u\t%u\t\t%u\n",i,(unsigned int)pNum,*pNum);
     pNum++;   //pNum = pNum + 1  << that is what ++ means in C
}
printf("\niNums     = %u\n",iNums);
printf("&iNums[0] = %u\n",(unsigned int)&iNums[0]);
printf("*iNums    = %u\n",*iNums);
puts("\nConclusion: An Array Name Without");
puts("The Brackets Is A Pointer To The");
puts("First Element Of The Array!");
getchar();

return 0;
}
/*
Output:

i       &iNums[i]       iNums[i]
================================
0       2293584         2
1       2293588         4
2       2293592         6
3       2293596         8
4       2293600         10
5       2293604         12


i       pNum            *pNum
================================
0       2293584         2
1       2293588         4
2       2293592         6
3       2293596         8
4       2293600         10
5       2293604         12

iNums     = 2293584
&iNums[0] = 2293584
*iNums    = 2

Conclusion: An Array Name Without
The Brackets Is A Pointer To The
First Element Of The Array!
*/