Structure
78 / 100

Unit-4 Structure – C Programming | BCA 2nd Sem

Unit-4 Structure – C Programming | BCA 2nd Sem – Hello everyone welcome to the pencilchampions.com website. This website provide Unit-4 Structure – C Programming | BCA 2nd Sem CCS University Notes. Thankyou for visiting.

Structure

Unit-5

Structure

Meaning of Structure

  • A structure is a user-defined data type in C that allows you to group together different variables of different data types under a single name. It’s like creating your own custom data type.
  • For example, let’s say you want to store information about a person such as their name, age, and occupation. You can define a structure called “Person” that contains variables for each of these pieces of information. Here’s an example:

C

struct Person {

    char name[50];

    int age;

    char occupation[50];

};

  • In this example, we define a structure called “Person” that has three variables: name (a character array of size 50 to store the person’s name), age (an integer to store the person’s age), and occupation (a character array of size 50 to store the person’s occupation).
  • Once you have defined a structure, you can create variables of that structure type and access its members using the dot (.) operator. Here’s an example:

Readmore- https://pencilchampions.com/unit-3-string-c-programming-bca-2nd-sem/


C

int main() {

    struct Person person1;

    strcpy(person1.name, “John”);

    person1.age = 25;

    strcpy(person1.occupation, “Engineer”);

    printf(“Name: %s\n”, person1.name);

    printf(“Age: %d\n”, person1.age);

    printf(“Occupation: %s\n”, person1.occupation);

    return 0;

}


Wikipedia- https://en.wikipedia.org/wiki/Struct_(C_programming_language)


 

  • In this example, we create a variable person1 of type struct Person. We then assign values to its members using the dot (.) operator. Finally, we print the values of the name, age, and occupation members.

The output of this program will be:

Name: John

Age: 25

Occupation: Engineer

How to create a structure

C

struct Person {

    char name[50];

    int age;

    char occupation[50];

};

  • In this example, we define a structure called “Person” using the struct keyword. Inside the curly braces, we list the variables that we want to include in the structure. Each variable has a data type, such as char or int, followed by a variable name.
  • Once you have defined a structure, you can create variables of that structure type. Here’s an example:

C

struct Person person1;

  • In this example, we create a variable called person1 of type struct Person. This variable will have the same structure as defined earlier, with variables for name, age, and occupation.
  • To access the members of a structure variable, you can use the dot (.) operator. Here’s an example:

C

strcpy(person1.name, “John”);

person1.age = 25;

strcpy(person1.occupation, “Engineer”);

  • In this example, we use the dot (.) operator to assign values to the name, age, and occupation members of the person1 variable.

How to declare Structure variables

  1. Define the structure using the struct keyword and specify the members inside the curly braces. For example:

C

struct Person {

    char name[50];

    int age;

    char occupation[50];

};

  1. Once the structure is defined, you can declare variables of that structure type. For example:

C

struct Person person1;

struct Person person2;

  1. After declaring the variables, you can access and assign values to their members using the dot (.) operator. For example:

C

strcpy(person1.name, “John”);

person1.age = 25;

strcpy(person1.occupation, “Engineer”);

strcpy(person2.name, “Jane”);

person2.age = 30;

strcpy(person2.occupation, “Teacher”);

  • In this example, we declared two variables person1 and person2 of type struct Person. We then assigned values to their respective members using the dot (.) operator.
  • Remember, the structure variables hold the data for each individual instance of the structure. You can declare as many structure variables as you need.

How to access structure elements

  • To access structure elements in C, you can use the dot (.) operator.
  • For example, if we have a structure called “Person” with members name, age, and occupation, and we have a variable person1 of type struct Person, we can access its elements like this:

C

printf(“Name: %s\n”, person1.name);

printf(“Age: %d\n”, person1.age);

printf(“Occupation: %s\n”, person1.occupation);

  • In this example, we use the dot (.) operator to access the name, age, and occupation members of person1.
  • Remember to use the appropriate format specifier when printing the values based on the data type of each member.

What is Structure pointer

  • A structure pointer is a pointer variable that can store the memory address of a structure. It allows you to indirectly access and manipulate the members of a structure.
  • To declare a structure pointer in C, you can use the following syntax:

C

struct Person {

    char name[50];

    int age;

    char occupation[50];

};

struct Person *personPtr;  // Declare a structure pointer

  • After declaring the structure pointer, you can assign the address of a structure variable to it using the address-of operator (&). For example:

C

struct Person person1;

personPtr = &person1;  // Assign the address of person1 to personPtr

  • Once the structure pointer is assigned the address of a structure variable, you can access the members of the structure using the arrow (->) operator. For example:

C

strcpy(personPtr->name, “John”);  // Access and modify the name member

personPtr->age = 25;  // Access and modify the age member

strcpy(personPtr->occupation, “Engineer”);  // Access and modify the occupation member

  • In this example, we use the arrow (->) operator to access and modify the members of the structure through the structure pointer.
  • Structure pointers are useful when you want to pass a structure to a function or dynamically allocate memory for a structure.

Nested Structures in C

  • Nested structures in C allow you to define a structure within another structure. This can be useful when you need to represent complex data structures.

Here’s an example of nested structures in C:

C

struct Address {

    char street[50];

    char city[50];

    char state[50];

    int zipCode;

};

struct Person {

    char name[50];

    int age;

    struct Address address; // Nested structure

};

int main() {

    struct Person person1;

    strcpy(person1.name, “John”);

    person1.age = 25;

    strcpy(person1.address.street, “123 Main St”);

    strcpy(person1.address.city, “New York”);

    strcpy(person1.address.state, “NY”);

    person1.address.zipCode = 10001;

    printf(“Name: %s\n”, person1.name);

    printf(“Age: %d\n”, person1.age);

    printf(“Address: %s, %s, %s, %d\n”, person1.address.street, person1.address.city, person1.address.state, person1.address.zipCode);

    return 0;

}

  • In this example, we have a structure called Address that represents a person’s address. The Person structure contains a member of type Address, which allows us to nest the Address structure within the Person structure.
  • By using nested structures, we can access and modify the members of the nested structure using the dot (.) operator. For example, person1.address.street refers to the street member of the nested Address structure.

Union in C

  • A union in C is a data structure that allows you to store different types of data in the same memory location. It is similar to a structure, but all members of a union share the same memory space.

Here’s an example of how to use a union in C:

C

union Data {

    int intValue;

    float floatValue;

    char stringValue[20];

};

int main() {

    union Data data;

    data.intValue = 10;

    printf(“Value: %d\n”, data.intValue);

    data.floatValue = 3.14;

    printf(“Value: %.2f\n”, data.floatValue);

    strcpy(data.stringValue, “Hello”);

    printf(“Value: %s\n”, data.stringValue);

    return 0;

}

  • In this example, we define a union called Data that has three members: intValue of type int, floatValue of type float, and stringValue of type char array.
  • We can assign values to these members and access them using the dot (.) operator, just like with structures. However, it’s important to note that only one member can be used at a time, and accessing a different member will overwrite the previous value.
  • Unions are useful when you want to save memory by storing different types of data in the same memory location, but you need to be careful with accessing and interpreting the values correctly.

Pointers to Union

  • Yes, you can use pointers to unions in C. Pointers allow you to store the memory address of a union, which gives you the ability to access and manipulate the union’s members indirectly.

Here’s an example of how to use pointers to unions in C:

c

union Data {

    int intValue;

    float floatValue;

    char stringValue[20];

};

int main() {

    union Data data;

    union Data *ptr;

    ptr = &data; // Assign the address of ‘data’ to the pointer

    // Accessing members using the pointer

    ptr->intValue = 10;

    printf(“Value: %d\n”, ptr->intValue);

    ptr->floatValue = 3.14;

    printf(“Value: %.2f\n”, ptr->floatValue);

    strcpy(ptr->stringValue, “Hello”);

    printf(“Value: %s\n”, ptr->stringValue);

    return 0;

}

  • In this example, we define a union called Data and create a pointer ptr of type union Data *. We assign the address of the data union to the pointer using the address-of operator &.
  • To access the members of the union using the pointer, we use the arrow operator ->. For example, ptr->intValue is equivalent to (*ptr).intValue.
  • Using pointers to unions can be helpful when you need to dynamically allocate memory for unions or when you want to pass unions to functions by reference.

Difference between structure and UNION

Structures and unions are both ways to define custom data types in C, but they have some key differences.

  1. Structures:
  • Structures allow you to group together different data types under one name.
  • Each member of a structure has its own memory space, and they can all be accessed simultaneously.
  • Structures are useful when you want to store and manipulate related data as a single entity.
  1. Unions:
  • Unions also allow you to group together different data types, but they share the same memory space.
  • Only one member of a union can be used at a time, and accessing a different member will overwrite the previous value.
  • Unions are helpful when you want to save memory by storing different types of data in the same memory location.
  • To summarize, structures are used to group related data types together, while unions are used to store different types of data in the same memory location. Structures provide separate memory space for each member, while unions share the same memory space for all members.

Discover more from

Subscribe to get the latest posts sent to your email.

By Atul Kakran

My name is Atul Kumar. I am currently in the second year of BCA (Bachelor of Computer Applications). I have experience and knowledge in various computer applications such as WordPress, Microsoft Word, Microsoft Excel, PowerPoint, CorelDRAW, Photoshop, and creating GIFs.

Leave a Reply

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading