Computer Concept & Programming In C - Unit IV - 8

Q.14 What are differences in Array and Structures? Explain with an example.                                                                  (AKTU. 2010 - 11)
Ans. The following are the differences between arrays and structures :
- Array elements are homogeneous. Structure elements are of different data type. 
- Array allocates static memory and uses index / subscript for accessing elements of the array. Structures allocate dynamic memory and uses (.) operator for accessing the member of a structure.
- Array is a pointer to the first element of it. Structure is not a pointer
- Array element access takes less time in comparison with structures.  
Examples :
You can give any one program of Array and any one program of Structures as example.

Q.15 What is pointer? How pointers are declared in C programming language? Illustrate with a suitable example.                                                    (AKTU. 2009-10)
Ans. A pointer is a variable whose value is used to point to another variable.
From this definition two things that follow are:
First that a pointer is a variable, so that you can assign different values to a pointer variable, and second that the value contained by a pointer must be an address that indicates the location of another variable in the memory. That is why a pointer is also called an address variable.
Address (left value) versus content (right value): -
As you might know, the memory inside your computer is used to hold the binary code of your program, which consists of statements and data, as well as the binary code of the operating system on your machine.
Each memory location must have a unique address so that the computer can read from or write to the memory location without any confusion. This is similar to the concept that each house in a city must have a unique address.
When a variable is declared, a piece of unused memory will be reserved for the variable, and the unique address to the memory will be associated with the name of the variable. The address associated with the variable name is usually called the left value of the variable.
Then, when the variable is assigned a value, the value is stored into the reserved memory location as the content. The content is also called the right value of the variable.
For instance, after an integer variable a is declared as 5 and assigned a value like this:
int a;
a = 5;
The variable a now hat two values:
Left value = 3791 & right value = 5
Here the left value is the address of the memory location reserved for a and left value = 3791. the right value 5 is the content stored in the memory location. Note that depending on the computers and operating systems the right value of a can be different from one machine to another.

Q.16 Describe pointer operations and declarations.
Related Questions -
Q. Write short note on pointer arithmetic.                                        (AKTU. 2013-14)
Ans. Pointer Operations and Declarations: -
Note that when your C program is being compiled and a value is being assigned to a variable, the C compiler has to check the left value of the variable. If the compiler cannot find the left value if will issue an error massage saying that the variable is undefined in your program.
That is why in C you have to declare a variable before you can use it. By using a variables left value, the C compiler can easily located the appropriate memory storage reserved for a variable, and then read or write the right value of the variable.
The address of operator (&): -
The C language even provides you with an operator, &, in case you want to know their left value of the variable. The operator is called the address-of-operator because it can return the address (that is the left value) of a variable.
For example the following code
long int x, y;
y = & x;
Assign the address of the long integer variable x to another variable y.
The De-reference Operator (*): -
In C * operator is called de- reference operator. It is both a uniary operator in this case and binary operator in case of performing multiplication (sometimes, it is also called the indirection operator). The value of a variable can be referenced by the combination of the * operator and its operand which contains the address of the variable.
For example: given an integer variable x and x = 1234, you can declare an integer pointer variable, ptr_x, for example, and assigns left value of x to ptr_x that is ptr_x = & x. Then the expression *ptr_x returns 1234, which is the right value (content) of x.
Declaring and assigning values to pointers: -
The general form of pointer declaration is 
char*ptr_c; /* declare a pointer to a character */
int * ptr_int; /* declare a pointer to an integer */
float * ptr_flt; /* declare a pointer to a floating-point */
# include <stdio.h>
# include <conio.h>
void main ( )
{
char c, *ptr_c;
int x, *ptr_x;
float y, *ptr_y;
c = ‘A’;
x = 7;
y = 123.45;
printf (“c: address = 0x%p, content = %c\n”, &c, c);
printf (“x: address = 0x%p, content = %d\n”, &x, x);
printf (“y: address = 0x%p, content = %5.2f\n”, &y, y);
ptr_c = &c;
printf (“ptr_c: address = 0x%p, content = 0x%p\n”, & ptr_c, ptr_c);
printf (“ * ptr_c => %^c\n”, *ptr_c);
ptr_x = &x;
printf (“ptr_x: address = 0x%p, content = 0x%p\n”, &ptr_x, ptr_x);
printf (“* ptr_x =>%d\n”, *ptr_x);
ptr_y = &y;
printf (“ptr_y: address = 0x%p, content = 0x%p\n”, & ptr_y, ptr_y );
printf (“ptr_y => %5.2f\n”, *ptr_y);
getch ( );
}
The output of the program is as follows:
c: address = 0x1B38, content = A
x: address = 0x1B36, content = 7
y: address = 0x1B32, content = 123.45
ptr_c: address = 0x1B30, content = 0x1B38
*ptr_c => A
ptr_x: address = 0x1B32E, content = x1B36
*ptr_x => 7
ptr_y: address = 0x1B2C, content = 0x1B32
*ptr_y => 123.45
Explanation: The statements in lines 8, 9 and 10 initialize three variables c, x and y.
The lines 11, 12 and 13 print out the address as well as the contents of the three variables. In line 14 the left value of the character variable c is assigned to the pointer variable ptr_c. the output made by the statement in line 15 shows that the variable ptr_c contains the address of c. In other words, the content (that is the right value) of ptr_c is the address (that is, the left value of c).
Then in line 16 the value referred to by the pointer *ptr_c is printed out. The output proves that the pointer *ptr_c does not point to the memory location of c.
Address arithmetic of pointers.

The memory image of variables and their pointers