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

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.

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.

Introduction to Object Oriented Programming ( OOP )

Object-Oriented Programming Object Oriented programming is a programing model that is based upon data and object. Classes   Classes are the blueprint of objects. Now you will think about what actually blueprints are. Blueprints are actually a design or plan to build something. Let's take an example like the map is detail plan of house classes are detail plan in  Object-Oriented programming. let's give you an example of a class on java so that you can unferstand. public class Car {          int horsePower;     String name;     String color;      String Company; } public class Sample {     public static void main(String[] args) {         Car car;         Car mehran;             } } Class name always start with capital letters . It is a good practice to name classes .  We will later learn how this is good. So in the above class Car ...