Question
: What are pointers ? What are the uses of
pointers in c ? How a pointer
variable is different from ordinary variable ? how pointer is useful in
declaring 2D array. Explain with suitable example.
Answer
:Pointer is a user defined
data type which creates special types of variables which can hold the address
of primitive data type like char, int, float, double or user defined
data type like function, pointer etc. or derived data type like array,
structure, union,enum. A pointer is a variable that contains the
address of another variable in memory.
A developer has to very carefully make use of pointers in
order to avoid some problems that can be nightmare to debug.
Example :
In the above declaration
int * ptr;
int a=5;
ptr=&a;
Declaration :
Syntax : <pointer_type>*<pointer_name>
1)
Pointer type : It specifies the type
of pointer. It can be int,float,etc. thus the type specifies the type of
variable whose address the pointer can store.
2)
Pointer name : It can be any name specified by the
user.
EG.
Char*ptr;
Int*ptr;
Float*ptr;
In the above declaration char, int , float signifies
the pointer type and ptr is the name of the pointer while the
asterisk (*) means it is a pointer
variable.
Initialization :
Pointers are initialized in the following way :
1.
<pointer_declaration>=<address of variable>
2.
<pointer_declaration><name of variable>=<address of
variable>
EG. Char ch=’c’;
Char*ptr=&ch;
#include<stdio.h>
int main()
{
int x=25;
int *ptr=&x; //statement one
int **temp=&ptr; //statement two
printf(“%d%d%d”.x.*ptr,**temp);
return 0;
}
Output: 25 25 25
Oridinary variable different from
pointer variable :
Pointers store the addresses of other
variables as well as other pointers(pointer to a pointer). On the other hand,
ordinary variablessimply store data values. A pointer is differenced using the
* operatorto refer to its data value. For example:
int a;
// this is an ordinaryvariable
int *p
= &a; // this is a pointer to the integer variable.
a and the & symbol inthe above example is
the address-of an operator. It returns the address of a variable a.
Pointer
useful in declaring 2D array
A 2D array is viewed as an array of 1D arrays.
That is, each row in a 2D array is a 1D array. Therefore given a 2D array A,
int
A[m][n]
we can
think of A[0] as the address of row 0, A[1] as address of row 1 etc..
Hence to find, say A[0][2] we do the following
:
A[0][2] = *(A[0] + 2)
In general, A[i][j] = *(A[i] + j)
The
syntax of writing 2-D array as 1-D array of pointers-
Data_type*arrayname(pointer
name)[size];
Here data type refers to data type of original
name whose name is array name and size is +ve integer value thet indicates the
max. elements associated with that array.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
int (*a)[4];
clrscr();
printf("ennter elements of array:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",(*(a+i)+j));
}
}
printf("output is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",(*(*(a+i)+j)));
}
printf("\n");
}
getch();
}
#include<conio.h>
void main()
{
int i,j;
int (*a)[4];
clrscr();
printf("ennter elements of array:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",(*(a+i)+j));
}
}
printf("output is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",(*(*(a+i)+j)));
}
printf("\n");
}
getch();
}
QUESTION 2 :DIFFERENCE B/W STRUCTURE AND
UNION IN C? HOW ARE THEY REPRESENTED IN C?
Structure
|
Union
|
1.The keyword struct is used to define a structure
|
1. The keyword union is used
to define a union.
|
2.syntax
struct struct_name {
structure element 1;
structure element 2;
----------
----------
structure element n;
}struct_var_nm;
|
2.syntax
union union_name {
union element 1;
union element 2;
----------
----------
union element n;
}union_var_nm;
|
2. When a variable is
associated with a structure, the compiler allocates the memory for each
member. The
size of structure is
greater than or equal to the sum of sizes
of its members. The smaller members may end with unused slack bytes.
|
2. When a variable is
associated with a union, the compiler
allocates the memory by
considering the size of the largest memory. So, size of union is equal to the
size of largest member.
|
3. Each member within a
structure is assigned unique storage area of location.
|
3. Memory allocated is
shared by individual members of union.
|
4. The address of each
member will be in ascending order This indicates that memory for each member
will start at different offset values.
|
4. The address is same for
all the members of a union. This indicates that every member begins at the
same offset value.
|
5 Altering the value of a
member will not affect other members of the structure.
|
5. Altering the value of any
of the member will alter other member values.
|
6. Individual member can be
accessed at a time
|
6. Only one member can be
accessed at a time.
|
7. Several members of a
structure can initialize at once.
|
7. Only the first member of
a union can be initialized.
|
|
|
9.Example:
struct item_mst {
int rno;
char nm[50];
}it;
|
9.Example:
union item_mst {
int rno;
char nm[50];
}it;
|
Representation of union in c:
union [union tag]
{
member definition1;
member definition2;
member definition3;
} [one or more union variables];
Example
:
#include <stdio.h>
#include <string.h>
union
Data
{
int
i;
float
f;
char str[20];
};
int
main( )
{
union
Data data;
printf(
"Memory size occupied by data : %d\n", sizeof(data));
return 0;
}
Representation
of structure in c :
struct Point
{
int x;
int
y;
};
Example :
#include<stdio.h>
#include<conio.h>
Struct person
{
Char name[20];
Int day;
%
Int year;
};
Main()
{
Struct person p1, p2;
Clrscr();
Printf(“\nenter values\n\n”);
Scanf(“%s%d%d%d”,p1.name,&p1.day,&p2.manth,&p1.year);
Scanf(“%s%d%d%d”,p2.day,&p2.day,&p2.month,&p2.year);
Printf(“\nvalues you entered \n”);
Printf(“\n%s : %d %d\n”,p1.name,p1.month,p1.year);
Printf(“\n%s : %d
%d”,p2.name.p2.day,p2.month,p2.year);
Getch();
}
No comments:
Post a Comment