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

Pointers in C

Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data. For example, using pointers is one way to have a function modify a variable passed to it. It is also possible to use pointers to dynamically allocate memory, which means that you can write programs that can handle nearly unlimited amounts of data on the fly--you don't need to know, when you write the program, how much memory you need. Wow, that's kind of cool. Actually, it's very cool, as we'll see in some of the next tutorials. For now, let's just get a basic handle on what pointers are and how you use them. What are pointers? Why should you care? Pointers are aptly name: they "point" to locations in memory. Think of a row of safety deposit boxes of various sizes at a local bank. Each safety deposit box will have a number associated with it so that you ca...

Types of Computer Networks(LAN,MAN,WAN)

Types of Computer Networks Computer networks are categorized according to: How they are organized physically. The way they are used. The distance over which they operate. Three main types of computer networks are as follows: LAN   (Local Area Network) WAN (Wide Area Network) MAN ( Metropolitan-Area Network) LAN (Local Area Network) LAN is the most common type of network . LAN stands for Local area Network . It covers a small area. Most LANs are used to connect computers in a single building or group of buildings. Hundreds or thousands of computer maybe connected through LAN. Typical LANs can be found in industrial plants, office buildings, and college or university campuses. LANs are capable of transmitting data at very fast rate. Data transmission speeds of LAN are 1 to 100 megabits per second. It is much faster than data transmission over a telephone line. LAN can transmit data in a limited distance. There is also a limit on the number of...

Ring topologies

Ring topologies are similar to bus topologies, except they transmit in one direction only from station to station. Typically, a ring architecture will use separate physical ports and wires for transmit and receive. Token Ring is one example of a network technology that uses a ring topology.