Array Advance

File name ==arrInitialization.c==
/*code for:  array initialization ways  */
#include<stdio.h>
int passbyVal(int i);
int passbyRef(int *p);

int main()
{
    printf("\n");
    /*1.way of array intialization*/
    int arr1[]={10,20,30};
     int i;
    for(i=0;i<3;i++)
    {
    printf("%u=%d \n",&arr1[i],arr1[i]);
    }

    printf("\n");
    /*2.way of array intialization*/
    int arr2[3]={40,50,60};

    int *p;
    //p=&arr2;
    p=arr2;
    for(i=0;i<3;i++)
    {
    printf("%u=%d ",&p[i],*p);
    printf("\n");
    p++;
    }
    printf("\n");
    /*1.way of array assignment*/
    int arr3[3];
    printf("Enter 3 elements 1 by 1:\n");
    for(i=0;i<3;i++)
    {
        scanf("%d",&arr3[i]);

    }
    for(i=0;i<3;i++)
    {
        printf("%u=%d\n",&arr3[i],arr3[i]);
    }
    printf("\n");
return 0;

}
/*

3218734580=10
3218734584=20
3218734588=30

3218734592=40
3218734600=50
3218734608=60

Enter 3 elements 1 by 1:
3218734604=2
3218734608=3
3218734612=4

*/
File name ==passingArray2.c==
/*
 code for:
Passing array to function.
->Pass by value means a copy is passed and any change in called does not affect calling func.
-> Pass by ref means a alias is passed so any change in the called function gets reflected in calling function
->Passing array means actually address of the first element is passed.
in both cases address of the first element of the array is passed
 */
#include<stdio.h>

/*ways of formal parameter for passing array to function*/
int func(int arr[]);
//int func(int arr[5])
//int func(int *arr);
int main()
{
int arr[5]={10,20,30,40,50};

func(arr);//whole address is passed ie strating address of array
//func(&arr);
int i;
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);
}

return 0;
}

int func(int arr[])
{
int i;
for(i=0;i<5;i++)
{
    arr[i]=arr[i]+5;/*as address of arr had been passed so change should be reflected in calling func*/
}
printf("\n");
}
/*
10 20 30 40 50
*/
\File name ==passingArray3.c==
/*
 code for:
Passing array to function.
->Pass by value means a copy is passed and any change in called does not affect calling func.
-> Pass by ref means a alias is passed so any change in the called function gets reflected in calling function
->Passing array means actually address of the first element is passed.
in both cases address of the first element of the array is passed
 */
#include<stdio.h>

/*ways of formal parameter for passing array to function*/
//int func(int arr[]);
//int func(int arr[5])
int func(int *arr);
int main()
{
int arr[5]={10,20,30,40,50};

func(arr);//whole address is passed ie strating address of array
//func(&arr);
int i;
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);
}

return 0;
}

int func(int arr[])
{
int i;
for(i=0;i<5;i++)
{
    arr[i]=arr[i]+5;/*as address of arr had been passed so change should be reflected in calling func*/
}
printf("\n");
}
/*
10 20 30 40 50
*/
\File name ==passingArray4.c==
/*
 code for:
Passing array to function.
->Pass by value means a copy is passed and any change in called does not affect calling func.
-> Pass by ref means a alias is passed so any change in the called function gets reflected in calling function
->Passing array means actually address of the first element is passed.
in both cases address of the first element of the array is passed
 */
#include<stdio.h>

/*ways of formal parameter for passing array to function*/
//int func(int arr[]);
//int func(int arr[5])
int func(int *arr);
int main()
{
int arr[5]={10,20,30,40,50};

func(arr);//whole address is passed ie strating address of array
//func(&arr);
int i;
for(i=0;i<5;i++)
{
printf("%d ",arr[i]);
}

printf("\n");
return 0;
}

//int func(int arr[])
int func(int *arr)
{
int i;
for(i=0;i<5;i++)
{
    //arr[i]=arr[i]+5;/*as address of arr had been passed so change should be reflected in calling func*/
    *(arr+i)=*(arr+i)+5;//same as above
}
}
/*
15 25 35 45 55
*/
File name ==passingArraybyVal.c==
/*
 code for:
 passing array by val or by ref.
 in both the case address of first element is passed and always pass by value only.
 */
#include<stdio.h>

void swap(int *arr);
/*
   OR

void swap(int arr[]);
void swap(int arr[2]);
*/


int main()
{
    int arr[3] = {1,2};
    printf("Before swap arr[0] = %d\n", arr[0]);
    printf("Before swap arr[1] = %d\n", arr[1]);  
    //swap(&arr);
    swap(arr);
    printf("After swap arr[0] = %d\n", arr[0]);
    printf("After swap arr[1] = %d\n", arr[1]);
}
void swap(int *arr)
{  
    int tmp = *arr;
    *arr = *(arr+1);
    *(arr+1) = tmp;
    /*
       int *next = arr + 1;
       *arr=*next;
       *next=tmp;
    */
}
/*
 Before swap arr[0] = 1
 Before swap arr[1] = 2
 After swap arr[0] = 2
 After swap arr[1] = 1
 */
File name ==passingArray.c==
/*
 code for:
Passing array to function.
->Pass by value means a copy is passed and any change in called does not affect calling func.
-> Pass by ref means a alias is passed so any change in the called function gets reflected in calling function
->Passing array means actually address of the first element is passed.
 */
#include<stdio.h>

/*ways of formal parameter for passing array to function*/
int func(int arr[]);
//int func(int arr[5])
//int func(int *arr);
int main()
{
int arr[5]={10,20,30,40,50};


printf("befor func arr elts were:\n");
int i;
for(i=0;i<5;i++)
{
    printf("%d ",arr[i]);
}
func(arr);
//func(&arr);// same as above as address of first element is passed in eithrt case

printf("After func arr elts were:\n");
for(i=0;i<5;i++)
{
    printf("%d ",arr[i]);
}

printf("\n\n");
return 0;
}

/*func definition. CHECK declarion variation*/
int func(int arr[])
{
int i;
for(i=0;i<5;i++)
{
    arr[i]=arr[i]+5;/*gets reflected in calling func in both cases by val or by ref */
}
printf("\n");
}
/*
befor func arr elts were:
10 20 30 40 50
After func arr elts were:
15 25 35 45 55
*/
\File name ==passingStructurebyRef.c==
/*
   code for:
   passing structur to function by ref,
   passing structs are similar to passing primitives. 
   If a struct is passed to a function the bytes of the struct are copied as the function parameter. 
   Anything done to that struct within the function changes the copy, not the original structure. 
 */


struct coordinate {
    int x;
    int y;
};
/*prototype of passing structur*/
void swap(struct coordinate *c);

int main()
{
    struct coordinate first;
    first.x = 1;
    first.y = 2;  
    printf("BEFORE swap first.x = %d\n", first.x);
    printf("BEFORE swap first.y = %d\n", first.y);  
   
    /*passing address of structure first to swap function*/
    swap(&first);
    printf("AFTER swap first.x = %d\n", first.x);
    printf("AFTER swap first.y = %d\n", first.y);
}
/*definition of  passing structure to function by accepting address by pointer but still it is pass by value only*/
void swap(struct coordinate *c)
{
    int tmp = c->x;
    c->x = c->y;
    c->y = tmp;
}


/*
BEFORE swap first.x = 1
BEFORE swap first.y = 2

AFTER swap first.x = 2
AFTER swap first.y = 1
*/
File name ==passingStructurebyValue.c==
/*
   code for:
   passing structur to function,
   passing structs are similar to passing primitives. 
   If a struct is passed to a function the bytes of the struct are copied as the function parameter. 
   Anything done to that struct within the function changes the copy, not the original structure. 
 */


struct coordinate {
    int x;
    int y;
};
/*prototype of passing structur*/
void swap(struct coordinate c);

int main()
{
    struct coordinate first;
    first.x = 1;
    first.y = 2;  
    printf("BEFORE swap first.x = %d\n", first.x);
    printf("BEFORE swap first.y = %d\n", first.y);  
   
    /*passing structure first to swap function*/
    swap(first);
    printf("AFTER swap first.x = %d\n", first.x);
    printf("AFTER swap first.y = %d\n", first.y);
}
/*definition of  passing structure to function*/
void swap(struct coordinate c)
{
    int tmp = c.x;
    c.x = c.y;
    c.y = tmp;
}


/*
BEFORE swap first.x = 1
BEFORE swap first.y = 2
AFTER swap first.x = 1
AFTER swap first.y = 2
*/
File name ==structureasUnion.c==
/*
   code for:
using structure as union
*/
#include<stdio.h>
#include<stdlib.h>
struct st
{
int jarr[0];
int j[10];
};

int main()
{
printf("\n\nsizeof structur=%d\n",sizeof(struct st));
struct st s1;
//printf("\naddress of i=%u\n",&s1.i);
printf("\naddress of jarr=%u\n",s1.jarr);
printf("\naddress of j=%u\n",&s1.j);

int i;
//intializing j
for(i=0;i<10;i++)
        {
        s1.j[i]=i;
        }
/*dereferencing by jarr works showing behavior of union */
for(i=0;i<10;i++)
{
printf("%d ",s1.jarr[i]);
}
return 0;
}
/*
 sizeof structur=40

 address of jarr=3213121108

 address of j=3213121108

 0 1 2 3 4 5 6 7 8 9

 */

File name ==1.c==
/*
   code for size of array and array element and pointer to array:

*/
#include<stdio.h>

int main()
{
int a=10;
printf("sizeof a=%d\n",sizeof(a));

int arr[1]={10};
printf("sizeof arr=%d\n",sizeof(arr));

char c='c';
printf("sizeof c=%d\n",sizeof(c));

char arrc[1]="c";
printf("sizeof arrc=%d\n",sizeof(arrc));
char arrd[1]="d"
//arrd[0]="d";
printf("arrd[0]=%c\n",arrd[0])
return 0;
}
/*
sizeof a=4
sizeof arr=4
sizeof c=1
sizeof arrc=1
arrd[0]=d
*/
\File name ==2.c==
/*
   code for:
using structure as union
*/
#include<stdio.h>
#include<stdlib.h>
struct st
{
int jarr[0];
int j[10];
};

int main()
{
printf("\n\nsizeof structur=%d\n",sizeof(struct st));
struct st s1;
//printf("\naddress of i=%u\n",&s1.i);
printf("\naddress of jarr=%u\n",s1.jarr);
printf("\naddress of j=%u\n",&s1.j);

int i;
for(i=0;i<10;i++)
        {
        s1.j[i]=i;
        }

for(i=0;i<10;i++)
{
printf("%d ",s1.jarr[i]);
}
char arr[0];
printf("\n\nsizeof[0]=%d\n",sizeof(arr));//0

char arr1[1]="c";
printf("sizeof[0]=%d\n",sizeof(arr1));//1

char arr2[2]="bc";
printf("sizeof[0]=%d\n",sizeof(arr2));//2

char arr3[]="qwery";
printf("sizeof[0]=%d\n",sizeof(arr3));//6=5+1

int *p1,*p2,*p3;
//printf("sizeof(p1)=%d\n",sizeof(p));//4
printf("p1=%u, sizeof(p1)=%d\n",p1,sizeof(p1));//4
printf("p2=%u, sizeof(p2)=%d\n",p2,sizeof(p2));//4
printf("p3=%u, sizeof(p3)=%d\n",p3,sizeof(p3));//4
printf("\n\n");

p1=malloc(0);
p2=malloc(0);
p3=malloc(0);
printf("p1=%u, sizeof(p1)=%d\n",p1,sizeof(p1));//4
printf("p2=%u, sizeof(p2)=%d\n",p2,sizeof(p2));//4
printf("p3=%u, sizeof(p3)=%d\n",p3,sizeof(p3));//4
printf("\n\n");


p1=malloc(1);
p2=malloc(1);
p3=malloc(1);
printf("p1=%u, sizeof(p1)=%d\n",p1,sizeof(p1));//4
printf("p2=%u, sizeof(p2)=%d\n",p2,sizeof(p2));//4
printf("p3=%u, sizeof(p3)=%d\n",p3,sizeof(p3));//4
printf("\n\n");



p1=malloc(2);
p2=malloc(2);
p3=malloc(2);
printf("p1=%u, sizeof(p1)=%d\n",p1,sizeof(p1));//4
printf("p2=%u, sizeof(p2)=%d\n",p2,sizeof(p2));//4
printf("p3=%u, sizeof(p3)=%d\n",p3,sizeof(p3));//4

printf("\n\n");
char *p;
p=malloc(8*sizeof(char));
printf("sizeof(p)=%d\n",sizeof(p));//4

p=arr3;
printf("p=%u and *p=%c \n",p,*p);
p++;
printf("p=%u and *p=%c\n",p,*p);
return 0;
}
\File name ==2d.c==
/*
   code for 2d arrays: two dimensional array in c

 */
#include<stdio.h>

int main()
{
   int arr2d[4][2]={0,1,        2,3,
        4,5,
        6,7
    };


    return 0;
}
\File name ==3d.c==
/*
   code for:
   array of 3 dimensional array

*/
#include<stdio.h>

int main()
{
   
     int a[3]={10,20,30};
     int* pa[]={a,a+1,a+2};
     int b[3]={40,50,60};
     int* pb[]={b,b+1,b+2};

     int* pab[]={a,b};
     int q;
     for(q=0;q<2;q++)
     printf("%d ",*pab[q]);
   
    int arr1[3][2][1]={{10,20},{30,40},{50,60}};
    int arr2[3][2][1]={{11,21},{31,41},{51,61}};
    int arr3[3][2][1]={{12,22},{32,42},{52,62}};
    int arr4[3][2][1]={{13,23},{33,43},{53,63}};
    int arr5[3][2][1]={{13,23},{33,43},{53,63}};


    //int*** ptr[4]= { arr1,arr2,arr3,arr4 };
    int*** ptr[]={arr5};
 
   
 
     int m,i,j,k;
     for (m=0;m<4;m++)
     {
         for(i=0;i<3;i++)
         {
             for(j=0;j<2;j++)
             {
                 for(k=0;k<1;k++)
                 {
                     printf("%d ",ptr[i][j][k]);
                     //printf("%d",*(  *(  *(  *(ptr+m) +i)  +j) +k)            );
                 }
             }
         }
     }
   
     printf("\n\n");
return 0;
}
File name ==4.c==
/*
   code for trap in c array:

*/
#include<stdio.h>

int main()
{
int a[]={10,20,30};

a[0]=30;
a++;
return 0;
}

/**code for endianness in c/
#include<stdio.h>
int main()
{
    int a[4]={0x2bce1c2a,0xace1fbab,0x3b3aa33a,0xa3b3b4d4};
    char *p=a;
    printf("[&p=%p][*p=%p]\n\n",&p,*p);
   
    //first elt
    int *i0=(int *)i0+0;//0xbfc6cc90 0x57e58955
    printf("[&i0=%p][*i0=%p]\n\n",&i0,*i0);
   
    //first elt after base ie second elt from start
    int *i1=(int *)a+1;
   printf("[&i1=%p][*i1=%p]\n\n",&i1,*i1);//0xbfc6cc94

    //second elt after base ie third elt from start
    int *i=(int *)a+2;//second elt after base ie third elt from start
    printf("[&i=%p][*i=%p]\n\n",&i,*i);//0xbfc6cc98

    //6th elt after base ie isventh elt from start
    int *j=(int *)a +6;//garbage as size is 4 out bound access

    printf("\nbefore i+3 &p=%p\n\n",&p);//0xbfc5cd78
    p = i + 3;
    printf("\nafter i+3 &p=%p\n\n",&p);//0xbfc5cd78


    printf("&a=%x\n",&a);
    printf("&a=%p\n",a);
    printf("%p %p %p\n",*i,*j,*p);

    printf("\n\n  %d  \n %d  \n  %d\n\n",(char *)i-(char *)a,(char *)j-(char *)a,(char *)p-(char *)a);
return 0;
}

No comments:

Post a Comment