Skip to main content

Structures and Unions


  1. Structure in C groups different data type under a common name
  2. struct is a keyword to declare a structure
  3. The size of the structure depends on the size of the different data types
  4. Structure declaration always ends with a semicolon
Example:
struct employee
{
char name[50];
int empid;
float salary;
};
The total size occupied by the above structure is 50 + 2 + 4 = 56 bytes (on TC compiler) and 50 + 4 + 4= 58 (on other compilers like GCC, etc). Again the size depends on the compiler.
GCC compiler always increase the size of structure which is divisible by 4 for faster code generation, so the above structure takes 60 bytes of memory
  1. Declaring a structure does not consume any memory, only when the structure object is created memory occupies
  2. Variables initialization will not be done during the structure declaration
The above syntax declaration does not consume memory, but the following do
int main()
{
struct employee e1;
}
Now e1 consumes memory of 60 bytes.
Simple Structure to get some details and print them to the screen
// GCC Compiler under windows 64bit
#include <stdio.h>
#include <conio.h>
/*  structures are declared outside any functions,
so that all the functions in the program can have access to it
*/
struct employee
{
       char name[50];
       int empid;
       float salary;
};
int main()
{
    struct employee e1;
    printf("enter the name, id and salary");
    scanf("%s %d %f",e1.name, &e1.empid, &e1.salary); 
    printf("The details entered are \n");
    printf("%s %d %f",e1.name, e1.empid, e1.salary);
    getch();
    return 0;
}
In the above program name, empid and salary are part of employee, so it should be associated with the member of structure, that’s why e1.name, e1.empid and e1.salary.

Array of Structures
//the following program gets the inputs of 3 employees like name, id and salary and compute the total salary taken by all the employees
#include <stdio.h>
#include <conio.h>
/*  structures are declared outside any functions,
so that all the functions in the program can have access to it
*/
struct employee
{
       char name[50];
       int empid;
       float salary;
};
int main()
{
    struct employee e[3];
    int i;
    float sum=0;
    printf("enter the name, id and salary");
    for(i=0;i<3;i++)
    {
    scanf("%s %d %f",e[i].name, &e[i].empid, &e[i].salary);
    sum=sum+e[i].salary;
    }
   
   
    printf("The details entered are \n");
    for(i=0;i<3;i++)
    printf("%s %d %f",e[i].name, e[i].empid, e[i].salary);
    printf("Total Salary of all 3 employees is %f",sum);
    getch();
    return 0;
}
Sample Output
Clipboard02

Arrays within Structures
//The following program gets the 2 student inputs like 3 subject marks and compute the total of all three subject marks
#include <stdio.h>
#include <conio.h>
/*  structures are declared outside any functions,
so that all the functions in the program can have access to it
*/
struct student
{
       int subjects[3];
       float total;
};
int main()
{
    struct student s[2];
    int i,j;
    printf("enter the 3 subject marks");
    for(i=0;i<2;i++)
    {
    s[i].total=0;
    for(j=0;j<3;j++)
    {
    scanf("%d",&s[i].subjects[j]);
    s[i].total += s[i].subjects[j];
    }
    printf("The total marks scored by student%d is %f",i+1,s[i].total);
    }
   
    getch();
    return 0;
}
Sample Output
clip2
 T S Pradeep Kumar

Comments

Popular posts from this blog

Installing ns3 in Ubuntu 22.04 | Complete Instructions

In this post, we are going to see how to install ns-3.36.1 in Ubuntu 22.04. You can follow the video for complete details Tools used in this simulation: NS3 version ns-3.36.1  OS Used: Ubuntu 22.04 LTS Installation of NS3 (ns-3.36.1) There are some changes in the ns3 installation procedure and the dependencies. So open a terminal and issue the following commands Step 1:  Prerequisites $ sudo apt update In the following packages, all the required dependencies are taken care and you can install all these packages for the complete use of ns3. $ sudo apt install g++ python3 python3-dev pkg-config sqlite3 cmake python3-setuptools git qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools gir1.2-goocanvas-2.0 python3-gi python3-gi-cairo python3-pygraphviz gir1.2-gtk-3.0 ipython3 openmpi-bin openmpi-common openmpi-doc libopenmpi-dev autoconf cvs bzr unrar gsl-bin libgsl-dev libgslcblas0 wireshark tcpdump sqlite sqlite3 libsqlite3-dev  libxml2 libxml2-dev libc6-dev libc6-dev-i386 libclang-dev llvm-

Installation of NS2 (ns-2.35) in Ubuntu 20.04

Installation of NS2 (ns-2.35) in Ubuntu 20.04 LTS Step 1: Install the basic libraries like      $] sudo apt install build-essential autoconf automake libxmu-dev Step 2: install gcc-4.8 and g++-4.8 open the file using sudo mode $] sudo nano /etc/apt/sources.list Include the following line deb http://in.archive.ubuntu.com/ubuntu bionic main universe $] sudo apt update $] sudo apt install gcc-4.8 g++-4.8 Step 3:  Unzip the ns2 packages to home folder $] tar zxvf ns-allinone-2.35.tar.gz $] cd ns-allinone-2.35/ns-2.35 Modify the following make files. ~ns-2.35/Makefile.in Change @CC@ to gcc-4.8 change @CXX@ to g++-4.8 ~nam-1.15/Makefile.in ~xgraph-12.2/Makefile.in ~otcl-1.14/Makefile.in Change in all places  @CC@ to gcc-4.8 @CPP@ or @CXX@ to g++-4.8 open the file: ~ns-2.35/linkstate/ls.h Change at the Line no 137  void eraseAll() { erase(baseMap::begin(), baseMap::end()); } to This void eraseAll() { this->erase(baseMap::begin(), baseMap::end()); } All changes made Step 4: Open a new termi

Square and Sinusoidal Waveform in 8051 Microcontroller

Waveforms in 8051 Microcontroller Square Wave To create a square wave generation using Delay. let us say we want to construct a 1khz square waveform the processor instruction cycle of 8051 is 1.085microseconds so for 1khz (1milli seconds =1/1khz), is 1ms/1.085microseconds = 921.6 (this value is set to the for loop) #include <reg51.h> void delay() { int i=0; for(i=0;i<922;i++) } void main() { P0=0xff; delay(); P0=0x00; delay(); } Sine Wave generation Since sine wave is plotted in a digital device the number of samples determines the smoothness, hence in this case, more the samples, smoother is the waveform. so a lookup table is been created to get the samples. in the following examples, there are totally 36 samples are taken, starting from 0 degrees to 360 degrees with a step of 10 degrees Degrees A = 5 (1+Sin theta) Where 5 is the full scale voltage DAC = 25.6 * A 0 5 128 30 7.5 192 60 9.3 239 90 10 256 etc…like this we need to calculate for 13 samples  with a ste