Archive for 15. Structure and union in C

Working with Pointers in c

what is pointer?  

In c,a pointer is a variable that points to or references a memory location in which data is stored. In the computer,each memory cell has an address that can be used to access that location so a pointer variable points to a memory location we can access and change the contents of this memory location via the pointer.


Pointer declaration:
A pointer is a variable that contains the memory location of another variable in shich data is stored. Using pointer,you start by specifying the type of data stored in the location. The asterisk helps to tell the compiler that you are creating a pointer variable. Finally you have to give the name of the variable. The syntax is as shown below.
type * variable name


The following example illustrate the declataion of pointer variable
int *ptr;
float *string;


Address operator:
Once we declare a pointer variable then we must point it to something we can do this by assigning to the pointer the address of the variable you want to point as in the following example:
ptr=#
The above code tells that the address where num is stores into the variable ptr. The variable ptr has the value 21260,if num is stored in memory 21260 address then


The following program illustrate the pointer declaration
/* A program to illustrate pointer declaration*/

main()
{
int *ptr;
int sum;
sum=45;
ptr=∑
printf (“\n Sum is %d\n”, sum);
printf (“\n The sum pointer is %d”, ptr);
}



Pointer expressions & pointer arithmetic:
In expressions,like other variables pointer variables can be used. For example if p1 and p2 are properly initialized and declared pointers, then the following statements are valid.
y=*p1**p2;
sum=sum+*p1;
z= 5* – *p2/p1;
*p2= *p2 + 10;
C allows us to subtract integers to or add integers from pointers as well as to subtract one pointer from the other. We can also use short hand operators with pointers p1+=; sum+=*p2; etc., By using relational operators,we can also compare pointers like the expressions such as p1 >p2 , p1==p2 and p1!=p2 are allowed.


The following program illustrate the pointer expression and pointer arithmetic
/*Program to illustrate the pointer expression and pointer arithmetic*/
#include< stdio.h >
main()
{ int ptr1,ptr2;
int a,b,x,y,z;
a=30;b=6;
ptr1=&a;
ptr2=&b;
x=*ptr1+ *ptr2 6;
y=6*- *ptr1/ *ptr2 +30;
printf(“\nAddress of a +%u”,ptr1);
printf(“\nAddress of b %u”,ptr2);
printf(“\na=%d, b=%d”,a,b);
printf(“\nx=%d,y=%d”,x,y);
ptr1=ptr1 + 70;
ptr2= ptr2;
printf(“\na=%d, b=%d,”a,b);
}


Pointers and function:
In a function declaration,the pointer are very much used . Sometimes,only with a pointer a complex function can be easily represented and success. In a function definition,the usage of the pointers may be classified into two groups.
1. Call by reference
2. Call by value.


Call by value:
We have seen that there will be a link established between the formal and actual parameters when a function is invoked. As soon as temporary storage is created where the value of actual parameters is stored. The formal parameters picks up its value from storage area the mechanism of data transfer between formal and actual parameters allows the actual parameters mechanism of data transfer is referred as call by value. The corresponding formal parameter always represents a local variable in the called function. The current value of the corresponding actual parameter becomes the initial value of formal parameter. In the body of the actual parameter,the value of formal parameter may be changed. In the body of the subprogram,the value of formal parameter may be changed by assignment or input statements. This will not change the value of the actual parameters.
/* Include< stdio.h >

void main()
{
int x,y;
x=20;
y=30;
printf(“\n Value of a and b before function call =%d %d”,a,b);
fncn(x,y);
printf(“\n Value of a and b after function call =%d %d”,a,b);
}

fncn(p,q)
int p,q;
{
p=p+p;
q=q+q;
}



Call by Reference:
The address should be pointers,when we pass address to a function the parameters receiving . By using pointers,the process of calling a function to pass the address of the variable is known as call by reference. The function which is called by reference can change the value of the variable used in the call.
/* example of call by reference*?

/* Include< stdio.h >
void main()
{
int x,y;
x=20;
y=30;
printf(“\n Value of a and b before function call =%d %d”,a,b);
fncn(&x,&y); printf(“\n Value of a and b after function call =%d %d”,a,b);
}
fncn(p,q)
int p,q;
{
*p=*p+*p;
*q=*q+*q;
}



Pointer to arrays:
an array is actually very much similar like pointer. We can declare as int *a is an address,because a[0] the arrays first element as a[0] and *a is also an address the form of declaration is also equivalent. The difference is pointer can appear on the left of the assignment operator and it is a is a variable that is lvalue. The array name cannot appear as the left side of assignment operator and is constant.
/* A program to display the contents of array using pointer*/
main()
{
int a[100];
int i,j,n;
printf(“\nEnter the elements of the array\n”);
scanf(%d,&n);
printf(“Enter the array elements”);
for(I=0;I< n;I++)
scanf(%d,&a[I]);
printf(“Array element are”);
for(ptr=a,ptr< (a+n);ptr++)
printf(“Value of a[%d]=%d stored at address %u”,j+=,*ptr,ptr);
}


Pointers and structures
We know the name of an array stands for address of its zeroth element the same concept applies for names of arrays of structures. Suppose item is an array variable of the struct type. Consider the following declaration:
struct products
{
char name[30];
int manufac;
float net;
item[2],*ptr;
this statement ptr as a pointer data objects of type struct products and declares item as array of two elements, each type struct products.


Pointers on pointer
A pointer contains garbage value until it is initialized. Since compilers cannot detect the errors may not be known until we execute the program remember that even if we are able to locate a wrong result,uninitialized or wrongly initialized pointers it may not provide any evidence for us to suspect problems in pointers. For example the expressions such as
*ptr++, *p[],(ptr).member

Leave a Comment

Structure and union in C

  • What is a Structure?
  • Structure is a method of packing the data of different types.
  • When we require using a collection of different data items of different data types in that situation we can use a structure.
  • A structure is used as a method of handling a group of related data items of different data types.


Syntax of Using Structure
structure definition:
general format:
struct tag_name
{
data type member1;
data type member2;
….

}


Example of Using Structure:
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
};
To holds the details of four fields namely title, author pages and price,the keyword struct declares a structure. These are the members of the structures. Each member may belong to same or different data type. The tag name can be used to define the objects that have the tag names structure. The structure we just declared is not a variable by itself but a template for the structure. We can declare the structure variables using the tag name any where in the program. For example the statement, struct lib_books book1,book2,book3; declares the book1,book2,book3 as variables of type struct lib_books each declaration has four elements of the structure lib_books. The complete structure declaration might look like this
The complete structure declaration might look like this
struct lib_books
{
char title[20];
char author[15];
int pages;
float price;
};struct lib_books, book1, book2, book3;


Get the Length of a Node List
The node list is always keeps itself up-to-date. If an element is deleted or added, in the node list or the XML document, the list is automatically updated.
The node list has a useful property called the length. The length property return the number of node in a node list.
The following code fragment get the number of <title> elements in “bookdetails.xml”:
struct lib_books { char title[20]; char author[15]; int pages; float price; }; struct lib_books, book1, book2, book3;
The following program shows the use of structure
/* Example program for using a structure*/
#include< stdio.h >
void main()
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}newstudent;
printf(”Enter the student information”);
printf(”Now Enter the student id_no”);
scanf(“%d”,&newstudent.id_no);
printf(“Enter the name of the student”);
scanf(“%s”,&new student.name);
printf(“Enter the address of the student”);
scanf(“%s”,&new student.address);printf(“Enter the cmbination of the student”);
scanf(“%d”,&new student.combination);printf(Enter the age of the student”);
scanf(“%d”,&new student.age);
printf(“Student information\n”);
printf(“student id_number=%d\n”,newstudent.id_no);
printf(“student name=%s\n”,newstudent.name);
printf(“student Address=%s\n”,newstudent.address);
printf(“students combination=%s\n”,newstudent.combination);
printf(“Age of student=%d\n”,newstudent.age);
}



Union:
However the members that we compose a union all share the same storage area within the computers memory where as each member within a structure is assigned its own unique storage area. Thus unions are used to observe memory. They are useful for the application involving multiple members. Where values need not be assigned to all the members at any time. Unions like structure contain members whose individual data types may differ from one another also. Like structures union can be declared using the keyword union as follows:
Last example will create a rectangle with rounded corner:
union item
{
int m;
float p;
char c;
}
code;
The notation for accessing a union member that is nested inside a structure remains the same as for the nested structure.In effect,a union creates a storage location that can be used by one of its members at a time. When a different number is assigned to a new value the new value supercedes the previous members value. Unions may be used in all the places where a structure is allowed.

Leave a Comment

Follow

Get every new post delivered to your Inbox.