Академический Документы
Профессиональный Документы
Культура Документы
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;
ptr2 = &x;
*ptr1 = 5;
*ptr2 = 10;
/* 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();
v.
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
struct info
{
char name[20];
int empid ;
} emp;
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;