Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Thursday, 6 February 2014

c-structure-using-pointer


C structure can be accessed in 2 ways in a C program. They are,
    1. Using normal structure variable
    2. Using pointer variable

Dot(.) operator is used to access the data using normal structure variable and arrow (->) is used to access the data using pointer variable.


Pointers can be accessed along with structures. A pointer variable of structure can be created as below:
struct name {
    member1;
    member2;
    .
    .
};
-------- Inside function -------
struct name *ptr;
 
Here, the pointer variable of type struct name is created.
Structure's member through pointer can be used in two ways:
  1. Referencing pointer to another address to access memory
  2. Using dynamic memory allocation
Consider an example to access structure's member through pointer.

#include <stdio.h>
struct name{
   int a;
   float b;
};
int main(){
    struct name *ptr,p;
    ptr=&p;            /* Referencing pointer to memory address of p */
    printf("Enter integer: ");
    scanf("%d",&(*ptr).a);
    printf("Enter number: ");
    scanf("%f",&(*ptr).b);
    printf("Displaying: ");
    printf("%d%f",(*ptr).a,(*ptr).b);
    return 0;
}

In this example, the pointer variable of type struct name is referenced to the address of p. Then, only the structure member through pointer can can accessed.
Structure pointer member can also be accessed using -> operator.
(*ptr).a is same as ptr->a
(*ptr).b is same as ptr->b

Accessing structure member through pointer using dynamic memory allocation

To access structure member using pointers, memory can be allocated dynamically using malloc() function defined under "stdlib.h" header file.

Syntax to use malloc()

ptr=(cast-type*)malloc(byte-size)
 
Example to use structure's member through pointer using malloc() function.
#include <stdio.h>
#include<stdlib.h>
struct name {
   int a;
   float b;
   char c[30];
};
int main(){
   struct name *ptr;
   int i,n;
   printf("Enter n: ");
   scanf("%d",&n);
   ptr=(struct name*)malloc(n*sizeof(struct name));
/* Above statement allocates the memory for n structures with pointer ptr pointing to base address */
   for(i=0;i<n;++i){
       printf("Enter string, integer and floating number  respectively:\n");
       scanf("%s%d%f",&(ptr+i)->c,&(ptr+i)->a,&(ptr+i)->b);
   }
   printf("Displaying Infromation:\n");
   for(i=0;i<n;++i)
       printf("%s\t%d\t%.2f\n",(ptr+i)->c,(ptr+i)->a,(ptr+i)->b);
   return 0;
}
Output
Enter n: 2
Enter string, integer and floating number  respectively:
Programming
2
3.2
Enter string, integer and floating number  respectively:
Structure
6
2.3
Displaying Information
Programming      2      3.20
Structure      6      2.30 

Example program for C structure using pointer:

           In this program, “record1″ is normal structure variable and “ptr” is pointer structure variable. As you know, Dot(.) operator is used to access the data using normal structure variable and arrow(->) is used to access data using pointer variable.

#include <stdio.h>
#include <string.h>

struct student
{
     int id;
     char name[30];
     float percentage;
};

int main()
{
     int i;
     struct student record1 = {1, "Raju", 90.5};
     struct student *ptr;

     ptr = &record1;    

         printf("Records of STUDENT1: \n");
         printf("  Id is: %d \n", ptr->id);
         printf("  Name is: %s \n", ptr->name);
         printf("  Percentage is: %f \n\n", ptr->percentage);

     return 0;
}

Output:

Records of STUDENT1:
Id is: 1
Name is: Raju
Percentage is: 90.500000

Example program to copy a structure in C:

There are many methods to copy one structure to another structure in C.
    1. We can copy using direct assignment of one structure to another structure or
    2. we can use C inbuilt function “memcpy()” or
    3. we can copy by individual structure members.
#include <stdio.h>
#include <string.h>

struct student
{
    int id;
    char name[30];
    float percentage;
};

int main()
{
    int i;
    struct student record1 = {1, "Raju", 90.5};
    struct student record2, *record3, *ptr1, record4;

    printf("Records of STUDENT1 - record1 structure \n");
    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",
            record1.id, record1.name, record1.percentage);

    // 1st method to copy whole structure to another structure
    record2=record1;   

    printf("\nRecords of STUDENT1 - Direct copy from " \
           "record1 \n");
    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",
            record2.id, record2.name, record2.percentage);

    // 2nd method to copy using memcpy function
    ptr1 = &record1;
    memcpy(record3, ptr1, sizeof(record1));

    printf("\nRecords of STUDENT1 - copied from record1 " \
           "using memcpy \n");
    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",
           record3->id, record3->name, record3->percentage);

    // 3rd method to copy by individual members
    printf("\nRecords of STUDENT1 - Copied individual " \
           "members from record1 \n");
    record4.id=record1.id;
    strcpy(record4.name, record1.name);
    record4.percentage = record1.percentage;

    printf("  Id : %d \n  Name : %s\n  Percentage : %f\n",
            record4.id, record4.name, record4.percentage);

     return 0;
}


Output:

Records of STUDENT1 - record1 structure
Id : 1
Name : Raju
Percentage : 90.500000  Records of STUDENT1 – Direct copy from record1
Id : 1
Name : Raju
Percentage : 90.500000
Records of STUDENT1 – copied from record1 using memcpy
Id : 1
Name : Raju
Percentage : 90.500000
Records of STUDENT1 – Copied individual members from record1
Id : 1
Name : Raju
Percentage : 90.500000

Monday, 3 February 2014

C Program to Add Two Complex Numbers using Structures in C

Way 1:

Without Paasing Structure to a Function:

#include <stdio.h>
#include <string.h>
struct complex{
    float real;
    float imag;
}complex;

main()
{
   
   struct complex n1,n2,temp;
    n1.real=10;
    n1.imag=10;
 n2.real=10;
    n2.imag=10;
 
      temp.real=n1.real+n2.real;
      temp.imag=n1.imag+n2.imag;
    printf("Sum=%.1f+%.1fi",temp.real,temp.imag);
    return 0;
}






Way 2:
by Passing Structure to a Function

#include <stdio.h>
struct complex{
    float real;
    float imag;
}complex;
complex add(complex n1,complex n2);
int main(){
    struct complex n1,n2,temp;
    printf("For 1st complex number \n");
    printf("Enter real and imaginary respectively:\n");
    scanf("%f%f",&n1.real,&n1.imag);
    printf("\nFor 2nd complex number \n");
    printf("Enter real and imaginary respectively:\n");
    scanf("%f%f",&n2.real,&n2.imag);
    temp=add(n1,n2);
    printf("Sum=%.1f+%.1fi",temp.real,temp.imag);
    return 0;
}
complex add(complex n1,complex n2){
      complex temp;
      temp.real=n1.real+n2.real;
      temp.imag=n1.imag+n2.imag;
}
     

Sunday, 2 February 2014

C code to convert any number to English word:

C code to convert any number to English word:

#include<stdio.h>
#include<string.h>

void toWord(int,int);
char * getPositionValue(int);
char * digitToWord(int);

char  word[100][30];
int i =0;

int main(){

    int j,k,subnumer;
    unsigned long int number;

    printf("Enter any postive number: ");
    scanf("%lu",&number);
   
    if(number ==0){
         printf("Zero");
         return 0;
    }

    while(number){

         if(i==1){
             toWord(number %10,i);
             number = number/10;
         }else{
             toWord(number %100,i);
             number = number/100;
         }

         i++;
        
    }

    printf("Number in word: ");
    *word[i-1] = *word[i-1] - 32;
    for(j=i-1;j>=0;j--){
         printf("%s",word[j]);
    }

    return 0;

}

void toWord(int number,int position){

    char  numberToword[100]={" "};

    if(number ==0){
    }else if (number < 20 ||number %10==0){
         strcpy(numberToword,digitToWord(number));
    }else{
         strcpy(numberToword,digitToWord((number/10)*10));
         strcat(numberToword,digitToWord(number%10));
    }
   
    strcat(numberToword,getPositionValue(position));
    strcpy(word[i],numberToword);
}

char * getPositionValue(int postion){

    static char positionValue[10]=" ";
   
    switch(postion){

         case 1: strcpy(positionValue,"hundreds "); break;
         case 2: strcpy(positionValue,"thousand "); break;
         case 3: strcpy(positionValue,"lakh "); break;
         case 4: strcpy(positionValue,"crore "); break;
         case 5: strcpy(positionValue,"arab "); break;
         case 6: strcpy(positionValue,"kharab "); break;
         case 7: strcpy(positionValue,"neel "); break;
         case 8: strcpy(positionValue,"padam "); break;
    }
    
    return positionValue;
}

char * digitToWord(int digit){

     static char digitInWord[10]=" ";

    switch(digit){
         case 1: strcpy(digitInWord , "one "); break;
         case 2: strcpy(digitInWord , "two "); break;
         case 3: strcpy(digitInWord , "three "); break;
         case 4: strcpy(digitInWord , "four "); break;
         case 5: strcpy(digitInWord , "five "); break;
         case 6: strcpy(digitInWord , "six "); break;
         case 7: strcpy(digitInWord , "seven "); break;
         case 8: strcpy(digitInWord , "eight "); break;
         case 9: strcpy(digitInWord , "nine ");break;
         case 10: strcpy(digitInWord , "ten "); break;
         case 11: strcpy(digitInWord , "eleven "); break;
         case 12: strcpy(digitInWord , "twelve "); break;
         case 13: strcpy(digitInWord , "thirteen "); break;
         case 14: strcpy(digitInWord , "fourteen "); break;
         case 15: strcpy(digitInWord , "fifteen "); break;
         case 16: strcpy(digitInWord , "sixteen "); break;
         case 17: strcpy(digitInWord , "seventeen "); break;
         case 18: strcpy(digitInWord , "eighteen "); break;
         case 19: strcpy(digitInWord , "nineteen "); break;
         case 20: strcpy(digitInWord , "twenty "); break;
         case 30: strcpy(digitInWord , "thirty "); break;
         case 40: strcpy(digitInWord , "fourty "); break;
         case 50: strcpy(digitInWord , "fifty "); break;
         case 60: strcpy(digitInWord , "sixty "); break;
         case 70: strcpy(digitInWord , "seventy "); break;
         case 80: strcpy(digitInWord , "eighty "); break;
         case 90: strcpy(digitInWord,"ninety "); break;
    }
   
    return digitInWord;
}

Wednesday, 29 January 2014

Dangling pointer

Dangling pointer:

If any pointer is pointing the memory address of any variable but after some variable has deleted from that memory location while pointer is still pointing such memory location. Such pointer is known as dangling pointer and this problem is known as dangling pointer problem.
Initially:


Later:

For example:


(q)What will be output of following c program?

#include<stdio.h>

int *call();
void main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);

}
int * call(){

int x=25;
++x;

return &x;
}


Output: Garbage value
Note: In some compiler you may get warning message returning address of local variable or temporary

Explanation: variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.

Solution of this problem: Make the variable x is as static variable.
In other word we can say a pointer whose pointing object has been deleted is called dangling pointer.

#include<stdio.h>

int *call();
void main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);

}
int * call(){

static int x=25;
++x;

return &x;
}

Output: 26

Near,Far,Huge pointer in C programming

Near pointer in C programming


In TURBO C there are three types of pointers. TURBO C works under DOS operating system which is based on 8085 microprocessor.

1. Near pointer
2. Far pointer
3. Huge pointer

Near pointer:

The pointer which can points only 64KB data segment or segment number 8 is known as near pointer.



That is near pointer cannot access beyond the data segment like graphics video memory, text video memory etc. Size of near pointer is two byte. With help keyword near, we can make any pointer as near pointer.

Examples:
(1)


#include<stdio.h>

int main(){

int x=25;
int near* ptr;

ptr=&x;
printf(“%d”,sizeof ptr);


return 0;
}

Output: 2

(2)


#include<stdio.h>

int main(){

int near* near * ptr;
printf(“%d”,sizeof(ptr),sizeof(*ptr));


return 0;
}

Output: 2 2
Explanation: Size of any type of near pointer is two byte.
Near pointer only hold 16 bit offset address. Offset address varies from 0000 to FFFF (in hexadecimal).

Note: In printf statement to print the offset address in hexadecimal, %p is used.

Example:


#include<stdio.h>

int main(){

int i=10;
int *ptr=&i;

printf("%p",ptr);


return 0;
}

Output: Offset address in hexadecimal number format.
%p is also used to print any number in hexadecimal number format.

Example:


#include<stdio.h>

int main(){

int a=12;
printf("%p",a);


return 0;
}

Output: 000C
Explanation: Hexadecimal value of 12 is C.

Consider the following two c program and analyze its output:

(1)


#include<stdio.h>

int main(){

int near * ptr=( int *)0XFFFF;
ptr++;
ptr++;
printf(“%p”,ptr);


return 0;
}

Output: 0003

(2)


#include<stdio.h>

int main(){

int i;
char near *ptr=(char *)0xFFFA;

for(i=0;i<=10;i++){
printf("%p \n",ptr);
ptr++;
}


return 0;
}

Output:

FFFA
FFFB
FFFC
FFFD
FFFE
FFFF
0000
0001
0002
0003
0004

Explanation: When we increment or decrement the offset address from maximum and minimum value respectively then it repeats the same value in cyclic order. This property is known as cyclic nature of offset address.

Cyclic property of offset address.

If you increment the near pointer variable then move clockwise direction. If you decrement the near pointer then move anti clockwise direction.