C H.W
Program is : (data memory) + (arithmetic/logic operation) Memories are numbered (address) where data is written and read. int a, b; This is called variable declaration and allocates memory for two integer spaces for a and b. Let’ assume a and b will get available memory slots: a @1000 and b @1004. So, if we do; a = 17; // store 17 @1000 b = a; // get the value stored @1000 and store it @1004 We will have: 1000 : 17 (same as saying ‘a’ has 17) 1004 : 17 (same as saying ‘a’ has 17) int *iptr; This means: allocate memory for ptr, which will contain an address where an integer will be stored. Let’s assume ptr is allocated at @1008. Now, memory looks like this: mem : value 1000 : 17 (same as saying ‘a’ has 17) 1004 : 17 (same as saying ‘a’ has 17) 1008 : ? (same as saying ‘iptr’ has ?) iptr = &a; ‘&a’ means the address of a. The a is allocated @1000, so &a is 1000. Now, the memory looks like this: mem : value 1000 : 17 (same as saying ‘a’ has 17) 1004 : 17 (same as saying ‘a’ has 17) 1008 : 1000 (same as saying ‘iptr’ has 1000) ‘ *iptr’ means access to what iptr has. The iptr has 1000. So, we go to 1000 and access data there. printf(“%d”, *iptr); will print 17, since that is what stored at 1000. If we do; iptr = &b; // iptr will contain b’s address, 1008 *iptr = *iptr + 3; // access to 1004 and get data (17), add 3, then store the result @1004
Then, the memory looks like; 1000 : 17 (same as saying ‘a’ has 17) 1004 : 20 (same as saying ‘a’ has 17) 1008 : 1004 (same as saying ‘iptr’ has 1000) If we can assign any integer address to iptr: iptr = &b; Now, printf(“%d”, *iptr); will print 20, since that is what stored at 1000. int array[10]; // a block of memory, each element (for example, array[3]) is an integer type. We can do; iptr = &array[2]; printf(“%d %d”, *iptr, array[2]); will print the same value. Because, iptr contains the address of array[2] and * means access to that address to get to the data. struct STUDENT { int sid; int tel; } struct STUDENT s0; struct STUDENT *sptr; sptr = &s0; Then, printf(“%d %d”, s0.sid, sptr->sid); will print the same value. s0.sid is a syntax to access sid of s0. If a pointer, sptr, is pointing to s0, then sptr->sid is the syntax.