Skip to main content

Pointers and structures in CPP

Pointers

       int *aptr;
       int a=10;
       aptr=&a;
       *aptr=a;               // here '*' is a valueAt operator.  So,
       cout<<aptr<<” and “ <<*aptr;

                o/p         10a87n   and 10


Passing an array to a function   without ptrs

       void fun(int []);
       int main()
       {
                int a[]={1,2,3,4,5};
                fun(a);
       }
       void fun(int x[])
      {        
              int j=0;
              int size=sizeof(x)/sizeof(*x);
              for(i=0;i<size;i++)
                    j+=x[i];
              cout<<j;
      }

Passing array to a function using ptr

fun(a);
void fun(int *x)
{              int j=0;                  Int size=sizeof(x)/sizeof(*x);
                for(i=0;i<size;i++)
                                j+=*(x+i)
                cout<<j;               }
note: int a[10];                  // size 40
                int *a[10];           //size 4

                b’cas *a is a single pointer tht points an array of 10 integers. So its size is 4 equals to a single integer pointer.

Passing arrays to a const array of a function
int main()
{
                int a[5]={1,2,3,4,5};
                fun(a);
}
void fun(congest int x[])
{
        int i=0;
        for(i=0;i<(sizeof(x)/sizeof(*x));i++)
        {
              cout<<x[i];        //x[i]=3;   it will generate an error like read only var can’t be changed. bcas x is a const
}
}

Structures
used to group diff.type of data under one name.
e.g
         struct name
         {
                int regno;            // structure members
                char[50] name;
         }a,b,*c;                     //this a,b are structure variables. And c is ptr to structure variable
                                           //Or  We can declare structure variables in main
         int main()
         {
                name a,b;
                a.regno=1;          //            assigning structure variables.
                c->regno=3;       //pointer to structure variable
                b={2,”gopal”};
                cout<<b.name<<a.regno<<endl;                             //accesing structure varuables
         }

Passing structure variable to a function

#include<iostream>       Using namespace std;
struct stu 
{
        int regno;
        char[10] name;
}
void display(stu s1,stu *s2);
int main() 
{
                stu aa={1,”g”};
                stu bb={2.”s”};       //aa.regno=1;
                display(aa);             //after called this function aa.regno=1; not changed;
                show(&bb);            //b4 calling  bb.regno=2;  after calling this bb.regno=1234; bcas pointer
                                                directly deals with memories.
}
void display(stu a)
{
      cout<< a.regno << a.name;        //a.regno=1
      a.regno=’1234’;                                //a.regno=1234
}
void show(stu *b) 
{
       cout<< b->regno << b->name;  //b->regno=2
       a.regno=’1234’;                         //b->regno=1234
}

Nested structure
      struct stu 
      {
            int a;
            string name;
      }
      struct result 
     {
            stu s;                     // structure inside another structure
            stu *p;
            int tot
     }
int main()
{
                result res,*res1;
                res.s.name=”hi”;
                res.p->name=”im”;
                res1->s.name=”go”;
                res1->p->name=”pal”;  //error  can’t assign like this
}

Sizeof() operator
struct stu {
                int a;
                string ab;
}
cout << sizeof(int);
used to allocate memory dynamically, returns address of the memory. That’s why we use pointer to catch that address.
cout << sizeof(stu);
Union
 Union is similar to structure. However, Union have a common memory among all its members. The union variable size is equal to the size of its largest sized member. For example,

union ak
{
      int a;
      char c;
} var1;
int main()
{
     var1.a=3;             //now the 4byte of union will hold value 3;
     cout << var1.a << var1.c;               both are 3;
     var1.c=’f’;            // now the value 3 is overwritten with the value ‘f’ b’cas union memory is
                                      shared among its members. so it can hold only its last modified var value;
     cout << var1.a << var1.c;               //now a and c are ‘f’
}

Here int size is 4 and char size is 1. So, the union size 4. And the union can hold a value of only one variable at a time.

Comments