Sie sind auf Seite 1von 5

Q) What is a pointer in C? How is a pointer variable declared? Give examples and explain.

Enumerate the utility of pointer variables.

Ans : A pointer is a variable that stores or holds the memory address of another
variable.
Pointers are declared almost in the same way as other variables, the only difference
is a asterisk added before its name a pointer. For example:
int *tash;

In the above example, the int indicates that the pointer tash will hold the address
of a integer variable.

If we want to store the address of a variable in the pointer tash, we use the
following code:
tash = &t;

the & symbol means the address of. Lets use a simple program to understand
this:

#include<stdio.h>
#include <conio.h>

void main()
{
clrscr();

int x, y;
int *ptr1, *ptr2; /*pointers declared; both will hold address of integer variables */
ptr1 = &y;

/* ptr1 holds the address of y */

ptr2 = &x;

/* ptr2 holds the address of x */

*ptr1 = 5;

/* y assigned the value 5 */

*ptr2 = 10;

/* x assigned the value 10 */

/* asterisk and pointer name displays the value of a variable */


printf("Value of X is: %d and Y is: %d\n", *ptr2, *ptr1);

/* just the mane of the variable displays the address of the variable */
printf("X is stored at memory location: %p and Y at: %p\n", ptr2, ptr1);

getch();

Q) A program in C language contains the following declaration: static int x[8] =


{1,2,3,4,5,6,7,8}; i) What is the meaning of x? ii) What is the meaning of (x + 2)? iii)
What is the meaning of *x? iv) What is the meaning of (*x + 2)? v) What is the
meaning of *(x + 2)?
Ans:
i.
ii.
iii.
iv.

v.

X represents the base address


(x+2) represents address of index two of the array.
*x represents the base value of array.
(*x+2) moves the pointer x by two. i.e. base address plus 2*2(memory
used to store a int variable). Finally it will represent the value in index two
of array.
*(x+2) represents the value stored at array index 2.

c) What is a structure?
Ans: structure is a collection of heterogeneous variables that has a name.
How does a structure differ from a union? Give examples.
Ans: In structures the elements have separate memory locations; but in unions they
all have a common storage spot. In unions we cannot use all the elements
simultaneously but is structures we can.
When we calculate the size of a structure, we add the size of all elements but in
union we only spot the largest element and use its size as size of the union.
Examples :

struct info
{
char name[20];
int empid ;
} emp;

Size of emp =
name[20] = 1*20 = 20 bytes
int empid= 2 bytes
total

= 20 +2 = 22 bytes

so the size of emp = 22 bytes.

struct info
{
char name[20];
int empid ;
} emp;

Size of union emp =


name[20] = 1*20 bytes
int empid = 2 bytes

total = max size = 20 bytes


so size of union emp = 20 bytes.

For structures:
This will works for structures

struct Info x;
x.name [20] = Test;
x. empid = 100;

For union
This will not works for structures

union Info x;
x.name [20] = Test;
x. empid = 100;

For what kind of applications, union data structure is useful?


Ans: Union data structure is used where we do not want to use all the elements
simultaneously.

How are arrays different from structure?


Ans: An array is a collection of homogeneous variables, where as a structure is a
collection of heterogeneous variables.

Das könnte Ihnen auch gefallen