Advance Structures in C

 code for: simple struct demo in c.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct emp
{
    int id;
    char firstName[5];//not good way
    char* secondName;//good way
    char* lastName;//best way when memory allocated
};

int main()
{
    struct emp *e1;
    e1=(struct emp*)malloc(sizeof(struct emp));
    e1->id=20;
    printf("emp id is:%d\n",e1->id);



/*1*/
   //e1->firstName="First First";//error:assgning * to array
    strcpy(e1->firstName,"First First");
    printf("emp first name is:%s\n",e1->firstName);

/*2*/
    e1->secondName="Second Second Second econdSecondSecond";
    //strcpy(e1->lastName,"SecondSecondSecond");//Seg fault
    printf("emp second name is:%s\n",e1->secondName);

/*3*/
    e1->lastName=(char *)malloc(1*sizeof(char));
    //e1->lastName="LastLastLast LastLastLast  LastLastLast";
    strcpy(e1->lastName,"LastLast LastLastLast  LastLastLastLast");
    printf("emp last name is:%s\n",e1->lastName);

    if(e1->lastName!=NULL)
    {
    printf("free1\n\n");
    free(e1->lastName);//crash
    e1->lastName=NULL;
    }
    if(e1!=NULL)
    {
        printf("free2\n\n");
    free(e1);
    e1=NULL;
    }
return 0;
}
/*
emp id is:20
emp first name is:First First
emp second name is:Second Second Second econdSecondSecond
emp last name is:LastLast LastLastLast  LastLastLastLast
free1

Example 2
#include<stdio.h>
#include<stdlib.h>
struct emp
{
int const i;
register int  rrr;
} e1;

int main()
{
//e1.i=10;
return 0;
}
Example:
*
   code for:
//ways of initializing structure members.

 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define SZ 4
struct emp
{
    int id;
    char name[20];
};

int main()
{

    /*way 1  initializing struct memb*/
    struct emp e2={20,"Kumar"};

    printf("id=%d, name=%s\n",e2.id, e2.name);


    /*way 2 of assiging struct memb*/
    struct emp e1;
    e1.id=10;
    strcpy(e1.name,"Ramesh");
    //strncpy(e1.name,"Ramesh",3);
    printf("id=%d, name=%s\n",e1.id, e1.name);

    /*way 3 Array of streuctures*/
    struct emp as[SZ];
    int i;
    for(i=0;i<SZ;i++)
    {
    printf("Enter id of structs member: %d\n",i+1);
        scanf("%d",&as[i].id);
    printf("Enter name of structs member: %d\n",i+1);
        scanf("%s", as[i].name);

    }

    for(i=0;i<SZ;i++)
    {
        printf("empid=%d name=%s\n",as[i].id,as[i].name);
    }
    return 0;
}
/*
 id=20, name=Kumar
 id=10, name=Ramesh
 Enter id of structs member: 1
 1111
 Enter name of structs member: 1
 Mahi
 Enter id of structs member: 2
 222
 Enter name of structs member: 2
 Mahita
 Enter id of structs member: 3
 3333
 Enter name of structs member: 3
 Aditya
 Enter id of structs member: 4
 4444
 Enter name of structs member: 4
 Adi
 empid=1111 name=Mahi
 empid=222 name=Mahita
 empid=3333 name=Aditya
 empid=4444 name=Adi

 */
/*
   code for:
   size of empty struct
   size of struct without declaration
*/
struct emp
{
   

};

int main()
{
    //printf("sizeof(struct):%d\n",sizeof(emp));//emp undeclared
    struct emp e1;
    printf("sizeof(struct):%d\n",sizeof(e1));//0

return 0;
}

/*
   code for:
   size of struct effect of padding, allignment of bits by compiler such that variables have equal length.
   size of member variable 
*/
#include<stdio.h>
#include<stdlib.h>
struct emp
{
    char gender;
    int id;
    char name[20];
};

int main()
{
    //printf("sizeof(struct):%d\n",sizeof(emp));//emp undeclared
    struct emp e1;
    printf("sizeof(struct):%d\n",sizeof(e1));//28=20+4+4 padding by compiler
    printf("sizeof(e1.id):%d\n",sizeof(e1.id));//4
    printf("sizeof(e1.name):%d\n",sizeof(e1.name));//20
    printf("sizeof(e1.gender):%d\n",sizeof(e1.gender));//1


return 0;
}
/*
   code for:
   size of struct effect of padding, allignment of bits by compiler such that variables have equal length.
   size of member variable 

*/
#include<stdio.h>
#include<stdlib.h>
struct emp
{
    //to avoid padding by compiler declare larger type first
    char name[20];
    int id;
    char gender;
};

int main()
{
    //printf("sizeof(struct):%d\n",sizeof(emp));//emp undeclared
    struct emp e1;
    printf("sizeof(struct):%d\n",sizeof(e1));//28=20+4+4 padding by compiler
    printf("sizeof(e1.id):%d\n",sizeof(e1.id));//4
    printf("sizeof(e1.name):%d\n",sizeof(e1.name));//20
    printf("sizeof(e1.gender):%d\n",sizeof(e1.gender));//1


return 0;
}
/*
   code for:
   simple struct

*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct emp
{
    int id;
    char firstName[20];//not good way
    char* secondName;//good way
    char* lastName;//best way when memory allocated
};

int main()
{
    struct emp *e1;
    e1=(struct emp*)malloc(sizeof(struct emp));
    e1->id=20;
    printf("emp id is:%d\n",e1->id);



/*1*/
    char fname[]="First first first";
   //e1->firstName="First First";//error:assgning * to array
    //strcpy(e1->firstName,fname);
//    memcpy(e1->firstName,fname,strlen(fname));
    strncpy(e1->firstName,fname,sizeof(e1->firstName));
    //printf("emp first name is:%s\n",e1->firstName);

/*2*/
    e1->secondName="Second Second Second econdSecondSecond";
    //strcpy(e1->lastName,"SecondSecondSecond");//Seg fault
    //printf("emp second name is:%s\n",e1->secondName);

/*3*/
    e1->lastName=(char *)malloc(100*sizeof(char));
    //e1->lastName="LastLastLast LastLastLast  LastLastLast";
    strcpy(e1->lastName,"LastLast LastLastLast  LastLastLastLast");
    //printf("emp last name is:%s\n",e1->lastName);
   


/*copying structure ising memcpy*/
    printf("\nprinting details of e1:\n");
    printf("%d\n%s\n%s\n%s\n",e1->id,e1->firstName,e1->secondName,e1->lastName);
//    struct emp *e2;
//    memcpy(&e2,&e1,sizeof(struct emp));
//    printf("\nprinting details of e2:\n"); printf("\n%d\n",e2->id); printf("%s",e2->firstName); printf("%s\n",e2->secondName); printf("%s\n",e2->lastName);



/*copying structure using = operator*/
    struct emp e3=*e1;
    printf("\nprinting details of e3:\n"); printf("\n%d\n",e3.id); printf("%s",e3.firstName); printf("%s\n",e3.secondName); printf("%s\n",e3.lastName);


   /*
    if(e1->lastName!=NULL)
    {
    printf("free1\n\n");
    free(e1->lastName);//crash
    e1->lastName=NULL;
    }
    if(e1!=NULL)
    {
        printf("free2\n\n");
    free(e1);
    e1=NULL;
    }
    */
return 0;
}
/*
emp id is:20
emp first name is:First First
emp second name is:Second Second Second econdSecondSecond
emp last name is:LastLast LastLastLast  LastLastLastLast
free1


 */
\File name ==s6.c==
/*
   code for:

*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct a
{
char ch[200];
char *p;
};

int main()
{
    struct a p1;
    strncpy(p1.ch,"Hello World",100);
    p1.p=malloc(100);
    strncpy(p1.p,"India",100);
   
    struct a p2=p1,p3 ;
    memcpy(&p3,&p1,sizeof(p3));
   
    printf("p1.ch=%s, p1.p=%s\n",p1.ch,p1.p);
    printf("p2.ch=%s, p2.p=%s\n",p2.ch,p2.p);
    printf("p3.ch=%s, p3.p=%s\n",p3.ch,p3.p);


    printf("p1.ch=%p, p1.p=%p\n",p1.ch,p1.p);
    printf("p2.ch=%p, p2.p=%p\n",p2.ch,p2.p);
    printf("p3.ch=%p, p3.p=%p\n",p3.ch,p3.p);

return 0;
}
\File name ==s7.c==
/*
   code for:
   simple struct

*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct emp
{
    int id;
    char firstName[20];//not good way
    char* secondName;//good way
    char* lastName;//best way when memory allocated
};

int main()
{
    struct emp e1;
    e1.id=20;
    printf("emp id is:%d\n",e1.id);



/*1*/
    char fname[]="First first first";
   //e1.firstName="First First";//error:assgning * to array
    //strcpy(e1.firstName,fname);
//    memcpy(e1.firstName,fname,strlen(fname));
    strncpy(e1.firstName,fname,sizeof(e1.firstName));
    //printf("emp first name is:%s\n",e1.firstName);

/*2*/
    e1.secondName="Second Second Second econdSecondSecond";
    //strcpy(e1.lastName,"SecondSecondSecond");//Seg fault
    //printf("emp second name is:%s\n",e1.secondName);

/*3*/
    e1.lastName=(char *)malloc(100*sizeof(char));
    //e1.lastName="LastLastLast LastLastLast  LastLastLast";
    strcpy(e1.lastName,"LastLast LastLastLast  LastLastLastLast");
    //printf("emp last name is:%s\n",e1.lastName);
   


/*copying structure ising memcpy*/
    printf("\nprinting details of e1:\n");
    printf("%d\n%s\n%s\n%s\n",e1.id,e1.firstName,e1.secondName,e1.lastName);
    struct emp e2;
    memcpy(&e2,&e1,sizeof(struct emp));
    printf("\nprinting details of e2:\n"); printf("\n%d\n",e2.id); printf("%s",e2.firstName); printf("%s\n",e2.secondName); printf("%s\n",e2.lastName);



/*copying structure using = operator*/
    struct emp e3=e1;
    printf("\nprinting details of e3:\n"); printf("\n%d\n",e3.id); printf("%s",e3.firstName); printf("%s\n",e3.secondName); printf("%s\n",e3.lastName);


   /*
    if(e1.lastName!=NULL)
    {
    printf("free1\n\n");
    free(e1.lastName);//crash
    e1.lastName=NULL;
    }
    if(e1!=NULL)
    {
        printf("free2\n\n");
    free(e1);
    e1=NULL;
    }
    */
return 0;
}
/*
emp id is:20
emp first name is:First First
emp second name is:Second Second Second econdSecondSecond
emp last name is:LastLast LastLastLast  LastLastLastLast
free1


 */
\File name ==s8.c==
/*
   code for:
   structure copy

*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct emp
{
char *name;  
};

int main()
{
struct emp e1;
char newname[]="Hello";
e1.name=(char*)malloc(strlen(newname));
strcpy(e1.name, newname);
printf("%s\n",e1.name);

struct emp e2,e3;

e2=e1;
printf("%s\n",e2.name);

memcpy(&e3,&e2,sizeof(struct emp));
printf("%s\n",e3.name);
return 0;
}
\File name ==s9.c==
/*
   code for:
   bit field

 */
#include<stdio.h>
#include<stdlib.h>


int main()
{
    struct emp
    {
        unsigned  int bit1:1;
        unsigned  int bit3:4;
        unsigned  int bit5:5;
    //    unsigned  int bit7:22;

    }bitt={1,17,32};
    printf("\n %u \n %u \n %u \n",bitt.bit1, bitt.bit3, bitt.bit5);



    struct emp2
    {
        int bit1:1;
        int bit3:4;
        int bit5:5;

    }bit={1,2,2};
    printf("\n%d\n%d\n%d\n",bit.bit1,bit.bit3, bit.bit5);

    return 0;
}
/*
 1
 1
 0

-1
2
2
 */

No comments:

Post a Comment