0% found this document useful (0 votes)
8 views13 pages

Understanding C Structs and Memory Size

The document provides examples and explanations of struct definitions in C, including how to declare, initialize, and access struct members. It also discusses the use of typedef for creating user-defined types and the implications of using pointers with structs. Various examples illustrate valid and invalid struct usage, memory size calculations, and member access methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Understanding C Structs and Memory Size

The document provides examples and explanations of struct definitions in C, including how to declare, initialize, and access struct members. It also discusses the use of typedef for creating user-defined types and the implications of using pointers with structs. Various examples illustrate valid and invalid struct usage, memory size calculations, and member access methods.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

struct tagname

{
DataType1 mem1;
DataType2 mem2;
DataType3 mem3;
};

Ex:
struct emp
{
int id;
char name[36];
int sal;
};
sizeof(emp)----->Error

sizeof(struct emp)----->40B(2+36+2)

Ex2:
struct student
{
int rno;
char sname[20];
char fname[20];
int marks[6];
int tmarks;
float avg;
};
sizeof(struct student)60B (2+20+20+12+2+4)

Ex: struct lib


{
int code;
char aname[30];
char bname[20];
char sname[20];
int rno;
};
sizeof(struct lib)--->74B
==================================================
struct tagname variable;

struct emp
{
int id;
char name[36];
int sal;
};
void main()
{
emp e1; //Error
struct emp e1,e2,e3;
}
sizeof(struct emp)---->40B

sizeof(e1)------>40B
==================================================
struct emp
{
int id;
char name[36];
int sal;
}e1,e2;
void main()
{
struct emp e3,e4;
}
sizeof(e1)---->40B

sizeof(e3)---->40B
==================================================

struct tagname *ptr_name;

struct emp
{
int id;
char name[36];
int sal;
}e1,*ptr1;
void main()
{
struct emp e2;
struct emp *ptr2;

ptr1=&e1;
ptr2=&e2;
}
sizeof(e1)---->40B

sizeof(ptr1)---->2B
=================================================
struct tagname var={mem1=value,mem2=value,...};

struct emp
{
int id;
char name[36];
int sal;
}e1={1,"Raj",12500},e2={2,"Sonu"};
void main()
{
struct emp e3={3};
struct emp e4;
}
=================================================

. struct to member

-> pointer to member

struct tagname variable;

[Link]=value;
[Link];

struct tagname *ptr;

ptr->member=value;

[Link];
==================================================
struct emp
{
int id;
char name[36];
int sal;
}e1={1,"Raj",12500},e2={2,"sonu"};
void main()
{
struct emp e3,e4,e5;

[Link]=3;
[Link]=sana; Error
[Link]="sana"; Error
strcpy([Link],"sana"); //yes
[Link]=12345;

[Link]=e3; Error
e2=[Link]; Error
[Link]=[Link]+1000; //yes

e4=e3; //yes

e1==e2; //Error
[Link]<[Link]; //yes
[Link]==[Link]; //yes

[Link]==[Link]; Error
strcmp([Link],[Link]);
}
==================================================
struct emp
{
int id;
char name[36];
int sal;
};
void main()
{
struct emp e1;
struct emp *ptr;
ptr=&e1;

[Link]=1;

e1->id=1; Error
[Link]=1; Error
ptr->id=1;

strcpy([Link],"Raj"); Error
strcpy(ptr->name,"Raj"); yes
strcpy([Link],"Raj");

[Link]=15000;
ptr->sal=1500;
}
==================================================
typedef

typedef DataType user_defined_name;

Ex1:

Ex2:
==================================================
struct
{
int id;
char name[36];
int sal;
};
is it valid? yes valid nameless structure
==================================================
struct
{
int id;
char name[36];
int sal;
}e1,e2;
void main()
{
struct tagname need?
}
==================================================
typedef struct
{
int id;
char name[36];
int sal;
}EMP;
void main()
{
EMP e1;
}
is it valid? yes valid
==================================================
typedef struct emp
{
int id;
char name[36];
int sal;
}EMP;
void main()
{
emp e1; Error
EMP e1; yes
struct emp e2;
}
==================================================
struct emp
{
int id;
char name[36];
int sal;
};
typedef struct emp EMP;
void main()
{
struct emp e1;
EMP e2;
}
==================================================
struct emp
{
int id ;
char rname[36];
int sal;
}e1,e2,e3; //yes valid

e1,e2 and e3 are global variables


==================================================
typedef struct emp
{
int id;
char name[36];
int sal;
}EMP e1,e2,e3; is it valid? Error
==================================================
typedef struct emp
{
int id;
char name[36];
int sal;
}EMP,e1,e2,e3; is it valid? yes valid
==================================================
struct e
{
int id;
char name[36];
int sal;
}e; is it valid? yes valid

=================================================
struct emp
{

}; is it valid? Error
==================================================
struct emp
{
int id=5;
char name[36]="Raj";
int sal=12500;
};
is it valid? Error
==================================================
struct emp
{
int id;
char name[36];
int sal;
struct emp e;
};
is it valid? Error
==================================================
struct emp
{
int id;
char name[36];
int sal;
struct emp *ptr;
};
sizeof(struct emp)----->42B
==================================================
struct emp
{
int id;
char name[36];
int sal;
};
void abc()
{
struct emp e1,e2;
}
void main()
{
struct emp e1,e2;
}
is it valid? yes
==================================================
void main()
{
struct emp
{
int id;
char name[36];
int sal;
};
struct emp e1,e2;
}
void abc()
{
struct emp e1,e2; //Error
}
is it valid? Error
==================================================
struct emp
{
int id;
char name[36];
int sal;
struct fac
{
char sub[20];
char pno[10];
}f;
};
void main()
{
struct fac f1; //sizeof(f1)--->30B
struct emp e1; //sizeof(e1)--->70B

[Link]=1; Error
strcpy([Link],"Raj"); Error
[Link]=10000; Error
strcpy([Link],"C&C++");
strcpy([Link],"123456789");

[Link]=1; //yes
strcpy([Link],"Raj"); //yes
[Link]=15000; //yes

strcpy([Link],"C&C++"); //Error
strcpy([Link],"C&C++");

strcpy([Link],"123456789");
}
==================================================
struct emp
{
int id;
char name[36];
int sal;
};
struct fac
{
struct emp e;
char sub[20];
char pno[10];
};
void main()
{
struct emp ob; //40B
struct fac f1; //70B

[Link]=101;
strcpy([Link],"Raj");
[Link]=9800;

[Link]=222; Error
[Link]=222;
strcpy([Link],"Kishore");
[Link]=12500;

strcpy([Link],"C&C++");
strcpy([Link],"134567890");
}

You might also like