computer-nec-license
  • NEC-Computer
  • 1. Concept of Basic Electrical and Electronics Engineering
    • 1.1 Basic Concepts
    • 1.2 Network Theorems
    • 1.3 Alternating Current Fundamentals
    • 1.4 Semiconductor Device
    • 1.5 Signal Generator
    • 1.6 Amplifiers
    • MCQs
      • MCQs On Basic Electrical
        • set-1
        • set-2
      • MCQs On Basic Electronics
        • set-1
        • set-2
  • 2. Digital Logic and Microprocessor
    • 2.1 Digital Logic
    • 2.2 Combinational & Arithmetic Circuit
    • 2.3 Sequential Logic Circuits
    • 2.4 Microprocessor
    • 2.5 Microprocessor System
    • 2.6 Interrupt Operations
    • MCQs
      • MCQs On Digital Logic
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
        • set-7
        • set-8
        • set-9
        • set-10
        • set-11
        • set-12
      • MCQs On Microprocessor
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
        • set-7
        • set-8
        • set-9
  • 3. Programming Language and Its Applications
    • 3.1 Introduction to C Programming
    • 3.2 Pointers, Structures, and Data Files
    • 3.3 C++ Language Constructs with Objects and Classes
    • 3.4 Features of Object-Oriented Programming
    • 3.5 Pure Virtual Functions and File Handling
    • 3.6 Generic Programming and Exception Handling
    • MCQs
      • set-1
      • set-2
      • set-3
      • set-4
      • set-5
  • 4. Computer Organization and Embedded System
    • 4.1 Control and CPU
    • 4.2 Computer Arithmetic and Memory System
    • 4.3 I/O Organization and Multiprocessor
    • 4.4 Embedded System Design
    • 4.5 Real-Time Operating and Control Systems
    • 4.6 Hardware Description Language (VHDL) and IC Technology
    • MCQs
      • set-1
      • set-2
      • set-3
      • set-4
      • set-5
      • set-6
      • set-7
      • set-8
      • set-9
      • set-10
      • set-11
  • 5. Concept of Computer Network and Network Security System
    • 5.1 Introduction to Computer Networks
    • 5.2 Data Link Layer
    • 5.3 Network Layer
    • 5.4 Transport Layer
    • 5.5 Application Layer
    • 5.6 Network Security
    • MCQs
      • Basic Networking
        • set-1
        • set-2
      • Advanced Networking
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
  • 6. Theory of Computation and Computer Graphics
    • 6.1 Introduction to Finite Automata
    • 6.2 Introduction to Context-Free Languages (CFL)
    • 6.3 Turing Machines (TM)
    • 6.4 Introduction to Computer Graphics
    • 6.5 Two-Dimensional Transformation
    • 6.6 Three-Dimensional Transformation
    • MCQs
      • MCQs on Theory of Computation
        • set-1
        • set-2
        • set-3
      • MCQs On Computer Graphics
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
  • 7. Data Structures and Algorithm, Database System and Operating System
    • 7.1 Introduction to Data Structures, Lists, Linked Lists, and Trees
    • 7.2 Sorting, Searching, Hashing and Graphs
    • 7.3 Introduction to Data Models, Normalization, and SQL
    • 7.4 Transaction Processing, Concurrency Control, and Crash Recovery
    • 7.5 Introduction to Operating System and Process Management
    • 7.6 Memory Management, File Systems, and System Administration
    • MCQs
      • MCQs ON DSA
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
      • MCQs On DBMS
        • set-1
        • set-2
      • MCQs On Operating System
        • set-1
        • set-2
        • set-3
        • set-4
        • set-5
        • set-6
        • set-7
        • set-8
        • set-9
        • set-10
        • set-11
        • set-12
  • 8. Software Engineering and Object-Oriented Analysis & Design
    • 8.1 Software Process and Requirements
    • 8.2 Software Design
    • 8.3 Software Testing, Cost Estimation, Quality Management, and Configuration Management
    • 8.4 Object-Oriented Fundamentals and Analysis
    • 8.5 Object-Oriented Design
    • 8.6 Object-Oriented Design Implementation
    • MCQs
      • set-1
      • set-2
      • set-3
      • set-4
      • set-5
      • set-6
      • set-7
      • set-8
      • set-9
  • 9. Artificial Intelligence and Neural Networks
    • 9.1 Introduction to AI and Intelligent Agents
    • 9.2 Problem Solving and Searching Techniques
    • 9.3 Knowledge Representation
    • 9.4 Expert System and Natural Language Processing
    • 9.5 Machine Learning
    • 9.6 Neural Networks
    • MCQs
      • set-1
      • set-2
      • set-3
      • set-4
      • set-5
      • set-6
      • set-7
      • set-8
      • set-9
  • 10. Project Planning, Design and Implementation
    • 10.1 Engineering Drawings and Its Concepts
    • 10.2 Engineering Economics
    • 10.3 Project Planning and Scheduling
    • 10.4 Project Management
    • 10.5 Engineering Professional Practice
    • 10.6 Engineering Regulatory Body
    • MCQs
      • MCQs On Engineering Drawing
        • set-1
        • set-2
      • MCQs On Engineering Economics
      • MCQs On Project Planning & Scheduling
      • MCQs On Project Mangement
      • MCQs On Engineering Professional Practice
      • MCQs On Engineering Regulatory Body
  • Questions Sets
    • Set 1 (Chaitra, 2080)
      • Short Questions (60*1=60 Marks)
      • Long Questions (20*2=40 Marks)
    • Set 2 (Aasadh, 2081)
      • Short Questions (60*1=60 Marks)
      • Long Questions (20*2=40 Marks)
    • Set 3 (Asojh, 2080)
      • Short Questions (60*1=60 Marks)
      • Long Questions (20*2=40 Marks)
    • Model Set - Computer Engineering By NEC
      • Short Questions (60*1=60 Marks)
      • Long Questions (20*2=40 Marks)
    • Model Set - Software Engineering By NEC
      • Short Questions (60*1=60 Marks)
      • Long Questions (20*2=40 Marks)
  • Tips & Tricks
Powered by GitBook
On this page
  • 1. Pointers
  • 2. Structures
  • 3. Unions
  • 4. File Operations
  • Conclusion
  1. 3. Programming Language and Its Applications

3.2 Pointers, Structures, and Data Files

1. Pointers

  1. What is a Pointer?

    • A pointer is a variable that stores the memory address of another variable.

    • Syntax:

      int a = 10;
      int *p = &a;  // 'p' is a pointer to 'a', storing its address.
      printf("%d", *p); // Dereference 'p' to access the value of 'a': 10
  2. Pointer Arithmetic

    • You can perform arithmetic operations on pointers to navigate through memory locations.

    • Valid operations:

      • Increment (p++): Moves to the next memory location of the same type.

      • Decrement (p--): Moves to the previous memory location.

      • Addition/Subtraction (p + n, p - n): Adjusts the pointer by n locations.

    • Example:

      int arr[] = {10, 20, 30};
      int *p = arr;     // Points to the first element of 'arr'.
      printf("%d\n", *p);  // Output: 10
      p++;               // Move pointer to the next element.
      printf("%d\n", *p);  // Output: 20
  3. Passing Pointers to Functions

    • Pointers allow functions to modify the original value of variables.

    • Example:

      void increment(int *n) {
          (*n)++; // Increment the value pointed to by 'n'
      }
      
      int main() {
          int num = 5;
          increment(&num);  // Pass the address of 'num'.
          printf("%d", num);  // Output: 6
          return 0;
      }

2. Structures

  1. What is a Structure?

    • A structure is a user-defined data type that groups variables of different types under a single name.

    • Syntax:

      struct Student {
          int id;
          char name[50];
      };
    • Declaration:

      struct Student s1;
      s1.id = 101;
      strcpy(s1.name, "Alice"); // Assign string to 'name'
      printf("ID: %d, Name: %s\n", s1.id, s1.name);  // Output: ID: 101, Name: Alice
  2. Pointer to a Structure

    • Accessing structure members using a pointer:

      struct Student s1 = {101, "Alice"};
      struct Student *ptr = &s1;
      printf("ID: %d, Name: %s\n", ptr->id, ptr->name);  // Output: ID: 101, Name: Alice
  3. Array of Structures

    • Example:

      struct Student {
          int id;
          char name[50];
      };
      
      struct Student students[2] = {{101, "Alice"}, {102, "Bob"}};
      printf("Student 1: %s\n", students[0].name);  // Output: Student 1: Alice
      printf("Student 2: %s\n", students[1].name);  // Output: Student 2: Bob

3. Unions

  1. What is a Union?

    • A union is similar to a structure but uses shared memory for all its members. Only one member can hold a value at any given time.

    • Syntax:

      union Data {
          int i;
          float f;
      };
      
      union Data d;
      d.i = 5;
      printf("%d\n", d.i);  // Output: 5
      d.f = 3.14;
      printf("%f\n", d.f);  // Output: 3.140000
  2. Key Difference from Structures

    • Structure: Allocates memory for all members.

    • Union: Allocates memory for the largest member only.


4. File Operations

  1. File Modes

    • "r": Open a file for reading.

    • "w": Open a file for writing (overwrites if the file exists).

    • "a": Open a file for appending (writes at the end without overwriting).

  2. Basic File Handling Functions

    • fopen(): Opens a file.

    • fclose(): Closes a file.

    • fprintf() and fscanf(): Handles formatted input/output.

    • fread() and fwrite(): Handles binary data.

  3. Examples

    • Writing to a File:

      include <stdio.h>
      int main() {
          FILE *file = fopen("data.txt", "w");  // Open file in write mode
          if (file == NULL) {
              printf("Error opening file!\n");
              return 1;
          }
          fprintf(file, "Hello, World!\n");  // Write to file
          fclose(file);  // Close file
          return 0;
      }
    • Reading from a File:

      #include <stdio.h>
      int main() {
          FILE *file = fopen("data.txt", "r");  // Open file in read mode
          char str[100];
          if (file == NULL) {
              printf("File not found!\n");
              return 1;
          }
          while (fscanf(file, "%s", str) != EOF) {  // Read words until EOF
              printf("%s\n", str);  // Print each word
          }
          fclose(file);  // Close file
          return 0;
      }
    • Binary File Handling:

      #include <stdio.h>
      struct Student {
          int id;
          char name[50];
      };
      
      int main() {
          FILE *file = fopen("student.dat", "wb");  // Open binary file for writing
          struct Student s = {101, "Alice"};
          fwrite(&s, sizeof(s), 1, file);  // Write structure to file
          fclose(file);  // Close file
      
          file = fopen("student.dat", "rb");  // Open binary file for reading
          struct Student readStudent;
          fread(&readStudent, sizeof(readStudent), 1, file);  // Read structure
          printf("ID: %d, Name: %s\n", readStudent.id, readStudent.name);  // Output: ID: 101, Name: Alice
          fclose(file);  // Close file
          return 0;
      }
  4. Common File Handling Errors

    • Forgetting to close a file.

    • Trying to read a file that doesn’t exist.

    • Using the wrong file mode.

Conclusion

  • Pointers, structures, unions, and file operations are essential concepts in C programming. Pointers provide direct memory manipulation and efficient data handling.

  • Structures enable grouping of related data, while unions optimize memory usage by sharing storage among members.

  • File operations facilitate data persistence and management through various modes and functions.

Previous3.1 Introduction to C ProgrammingNext3.3 C++ Language Constructs with Objects and Classes

Last updated 5 months ago