Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

how to use struct?

+15 votes
asked Nov 27, 2025 by Studenty (270 points)

3 Answers

+3 votes
answered Nov 27, 2025 by Peter Minarik (101,340 points)

A structure holds data that logically belong together. E.g. a person would have a name and an age and we'd always need to handle this information together. Then we can define a Person structure. For ease of use, we can also create a type definition: TPerson.

The code would look like this in C:

#include <stdio.h>

struct Person
{
    char name[64];
    unsigned char age;
};

typedef struct Person TPerson;

void PrintPerson(TPerson person)
{
    printf("Name: %s\n", person.name);
    printf("Age:  %d\n", person.age);
}

int main()
{
    struct Person person = { "John Doe", 30 };

    PrintPerson(person);

    return 0;
}

One can also unify the structure definition and the type definition by creating an anonymous structure and assigning a name to it via typedef:

typedef struct
{
    char name[64];
    unsigned char age;
} Person;

Structures only hold data, no functionality. For functions, you'd need a class, which is a C++ feature (and other object-oriented languages).

0 votes
answered Dec 20, 2025 by Nematjon Orifov (250 points)
#include <stdio.h>

struct Person
{
    char name[64];
    unsigned char age;
};

typedef struct Person TPerson;

void PrintPerson(TPerson person)
{
    printf("Name: %s\n", person.name);
    printf("Age:  %d\n", person.age);
}

int main()
{
    struct Person person = { "John Doe", 30 };

    PrintPerson(person);

    return 0;
}
0 votes
answered Dec 20, 2025 by Nematjon Orifov (250 points)
typedef struct
{
    char name[64];
    unsigned char age;
} Person;
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...