Skip to main content

Structures in C

Structure is user defined data type which is used to store heterogeneous data under unique name. Keyword 'struct' is used to declare structure.
We already know that arrays are many variables of the same type grouped together under the same name.

Similarity with arrays

Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create a structure called person which is made up of a string for the name and an integer for the age. Here is how you would create that person structure in
struct person
{

    char *name;
    int age;
};
The above is just a declaration of a type. You must still create a variable of that type to be able to use it. Here is how you create a variable called p of the type person:
Code:

#include<stdio.h>

struct person
{

    char *name;
    int age;
};

int main()
{
    struct person p;
    return 0;
}
To access the string or integer of the structure you must use a dot between the structure name and the variable name.
Code:

#include<stdio.h>

struct person
{

    char *name;
    int age;
};

int main()
{
    struct person p;
    p.name = "John Smith";
    p.age = 25;
    printf("%s",p.name);
    printf("%d",p.age);
    return 0;
}

Comments

Popular posts from this blog

Computer Buses

Computer Buses   A computer system consists of different devices.CPU must be able to communicate with all devices. The devices are connected together by a communications channel called bus. A bus consists of a set of communication lines or wires. It is used to move a large amount of bits in the form of electrical pulses from one unit to another. The bus is used to connect the following units: Central Processing Unit Control Unit  Arithmetic and Logic Unit Main Memory ( RAM, ROM) Input / Output Devices Bus is a common path to transfer data and commands between CPU, memory and input / output devices.It is also used to send or receive data from secondary storage.The capacity of a bus depends on the number of data lines in it.A bus with 16 lines can carry 16 bits or 2 bytes at a time. A bus with 32 lines can carry 32 bits or 4 bytes at a time . Types Of  Buses Different types of buses are as follows: 1. Data Bus 2.Addres...

Steps of splitting pdf files

Goto https://www.ilovepdf.com/split_pdf Click on Select PDF File. Upload your pdf file here. Select Extract Pages from right menu. Click on Split pdf button and wait for the procedure. Now Click on Download Split PDF and you will get a zip file in which there will be separate pdf.

Steps to remove google accounts from Computer

Open Google . You will see a round shaped picture of google account picture in top right corner as marked in below picture Click on it. Click on sign out of all accounts Click on Sign In at the top right corner as shown in picture below. Click on it. You will see following screen. Select your desired account from it and sign in . Reopen your form by clicking link provided to you, It will be open now.