Skip to the content
onlineexamguide
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login
  • Home
  • Courses
  • Engg. Interview
    • Placement Papers
    • Electrical Engineering
    • Mechanical Engineering
    • Automobile Engineering
    • Civil Engineering
    • Computer Science Engineering
    • Chemical Engineering
  • Online Exam
    • NTA UGC NET Exam
    • SSC Examination quiz
    • TET Examination Quiz
    • Banking Exam
    • Aptitude Tests
    • Computer Knowledge Tests
    • Logical Reasoning Tests
    • English Language Tests
    • Staff Nurse Exams
    • General Knowledge Tests
    • Networking Tests
  • Ghatna Chakra
  • Register
    • Instructor Registration
    • Student Registration
  • User Login

Structures AND Pointers in C Tutorial

Structures in C and pointers in c

Learn Structures AND Pointers in C MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests

A C language Structure is a user define data type used to combine similar or different data types as one single entity.

An int variable contains only integer data. A float variable contains only real number data. But a structure can be defined to hold multiple type data and refer to the elements separately.

Structures and Pointers in C

Keyword used to define a Structure Data Type is ‘struct’.

Two operators namely DOT Operator and ARROW Operator are used along with structures to access elements.

Syntax of Structure

struct name
{
  //different data type declarations
  //int year;
}var_1, var_2, *var_3;

Structures AND Pointers Observations

  1. To reuse structure data type, a name may be given to a structure at the time of declaration itself.
  2. A structure may contain different types of data like int, float, double, char, arrays and pointers.
  3. If you do not want to reuse structure data type, you can define some STRUCTURE VARIABLES along with declaration it self without STRUCTURE NAME.
  4. Structure naming convention follows same rules of defining a variable name. It should not be a keyword.
  5. You can omit structure NAME declaring just structure variables like var_1 etc.
  6. and You should use DOT (.) operator to access structure elements or members like var_1.year
  7. You should use ARROW (->) operator to access structure elements if the variable is a POINTER, var_3->year.
  8. Important observation is that a STRUCTURE definition should END WITH A SEMICOLON (;).

Pointers in C

Example 1

#include<string.h>
#include<stdio.h>
int main()
{
  struct mycar
  {
    char col[10], int year;
  };
  struct mycar car1;
  strcpy(car1.col, "BLACK");
  car1.year = 2019;
  printf("COLOR: %s, Year=%d", car1.col, car1.year);

  return 0;
}

Structures AND Pointers in c

Example 2: Using a Structure and Pointer Variable

#include<string.h>
#include<stdio.h>
int main()
{
  struct mycar
  {
    char col[10], int year;
  }*car5;
  //ARROW operator makes the difference.
  strcpy(car5->col, "RED");
  car5.year = 2020;
  printf("COLOR: %s, Year=%d", car5->col, car5->year);

  return 0;
}

C Structure elements in Memory

Declaring a structure does not allocate memory. Only when you create a structure variable, memory is allocated. Size of structure is the combined size of all data types in that structure.

struct mycar
{
  char col[10], int year;
};
//Size of structure = 10 bytes + 2 bytes = 12B

In the above example, size of a structure is the combined size of 10 char elements and one int element. It is 12 Bytes in total. Declaring this in a c program does not reserve memory of 12B.

struct mycar
{
  char col[10], int year;
};

struct mycar car1;
//this line reserves memory of 12B

Creating a structure variable car1 reserves the actual memory required to hold all car1 elements.

Structures AND Pointers Note: To create a STRUCTURE VARIABLE, keyword struct is used again.

Structure elements are aligned in contiguous memory locations like storing arrays. To store the next element type of a structure in memory, memory location starting with a multiple of 4 or multiple of 8 is chosen. So there may be a gap of 3 bytes or 7 bytes if 1B element is stored first.

A preprocessor directive #pragma pack is used to tell the compiler to choose gap between two elements. #pragma pack(1) tells the compiler to store the element in next byte. So the gap of 3 bytes or 7 bytes may be avoided. This is called Packing of Structure Elements.

C Structure Initialization and Copying

Structure elements can be initialized either individually or in one go at a time.

struct mycar
{
  char class;
  int year; float weight;
}car1;

Initializing structure elements individually

car1.class = 'B';
car1.year = 2019;
car1.weight = 350.5f; //kg

Initializing all structure elements in one go

struct mycar car2 = {'C', 2020, 275f };
// (OR)
struct mycar car3;
car3 = car2;

You can copy entire structure variable into another variable by using ASSIGNMENT OPERATOR ‘=’. You can initialize a structure variable using BRACES { } similar to initializing an array. Remember that you need to both declare and initialize elements at the same time using Braces

You can initialize all elements of a structure variable to Zeros or Null values using { 0 } definition. First element of the structure may be of any data type.

struct mycar car4 = { 0 };
// first element may be of any data type

Default value of a number data type is ZERO and character type data is NULL or ‘\0’.

Structure Variable Arrays in c

You can declare Structure variables like any other data type variables. You can use index starting from ZERO to refer to structure array variables.

struct student
{
  char name[10]; int age;
}stu[3];
stu[0].age = 25;
stu[1].age = 27;

Nested Structures in c

You can nest a structure inside another structure. Accessing a nested structure member is achieved using the same DOT (.) operator and ARROW (->) operators. YOu can initialize the elements at the time of declaration of structure variable it self. Only caution is that you should maintain order of elements and their values between Braces or FLOWER BRACKETS.

int main()
{
  struct engine
  {
    int capacity;
  };

  struct mycar
  {
    struct engine eg;
    int model; 
  };
  struct mycar car1;
  car1.eg.capacity = 2; //Litre
  car1.model = 2019;

  //initializing in one go
  //maintain the order of elements
  struct mycar car2 = {3, 2020};

  return 9;
}

[WpProQuiz 39]

Write a comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Atal Bihari Vajpayee | श्री अटल बिहारी वाजपेयी जीवन परिचय
  • सम्राट अशोक का जीवन परिचय (Emperor Ashoka)
  • Prithviraj Chauhan
  • बाल गंगाधर तिलक
  • स्वामी दयानंद सरस्वती
  • UPPSC Mines Inspector Recruitment 2022 Notification Out
  • AIIMS Delhi JR Vacancy 2022 [194 Post] Notification and Apply Online
  • भीमराव अम्बेडकर
  • डॉक्टर राजेंद्र प्रसाद का जीवन परिचय
  • श्रीनिवास रामानुजन का जीवन परिचय
  • Amnesty International day
  • World Economic Forum
  • UPSSSC VDO Syllabus and Exam Pattern 2022
  • RBI Officer Grade B Recruitment 2022
  • UKMSSB Assistant Professor Recruitment 2022 Apply Now 339 Post

onlineexamguide

onlineexamguide.com is the ultimate guide that will keep you updated about almost every Exam & Interviews . We aim to provide our readers with an informative details that have been occurring in Examination . Here at onlineexamguide.com , we focus on delivering our readers with the latest exam Pattern Mock test

We Provide Free online test to practice for Competitive exams , Online Exam, Entrance and Interview. Learn and Practice online test for Free and Prepare for your exam online with us

Quick links

  • About us
  • Privacy Policy
  • Instructor Registration
  • Student Registration
  • Java Programming Tests
  • C programming Tests
  • C++ programming Tests
  • Aptitude Tests

Follow us

Free Online Mock Test

  • UPTET PRIMARY Online Test Series
  • Super TET Mock Test in Hindi 2022
  • CTET Mock Test 2022 Paper 1
  • SSC CHSL Online Mock Test
  • SSC MTS Mock Test 2022
  • SSC CGL Mock Test
  • SSC GD Mock Test
  • ccc online test

Search

Learn and Earn

Register as Instructor - Create and sell online courses and coaching services with the best online platform onlineexamguide.com . Build a course, build a brand, earn money

Contact us

For any queries

Email us on - admin@onlineexamguide.com

We will response fast as much as we can
Copyright © 2022 onlineexamguide.com - All Rights Reserved.
error: Content is protected !!

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.