Skip to main content

Featured

C++ Program to Perform Linear Search on a Vector

  C++ Program to Perform Linear Search on a Vector Introduction In this C++ program, we will learn how to perform a Linear Search on a vector. The program first takes the size of the vector and its elements as input. Then it asks the user for the element to search. If the element is found, it displays the index where it is located. Otherwise, it displays a message indicating that the element is not found. C++ Program #include<bits/stdc++.h> using namespace std; int main() { int num, search, found = 0; cout << "Enter the size of the vector:" << endl; cin >> num; vector<int> v(num); cout << "Enter " << num << " elements in vector:" << endl; for(int i = 0; i < num; i++) { cin >> v[i]; } cout << "Enter element that you want to search:" << endl; cin >> search; for(int i = 0; i < num; i++) { ...

typedef Examples in C

typedef Examples in C

✅ Typedef Examples in C

1. Typedef with Pointers

#include <stdio.h>

typedef int* IntPtr;

int main() {
    int a = 100;
    IntPtr p = &a;  // same as int *p = &a;
    printf("Value at pointer: %d\n", *p);
    return 0;
}
  

2. Function Pointer with Typedef

#include <stdio.h>

typedef void (*FuncPtr)(int);

void greet(int x) {
    printf("Hello %d times!\n", x);
}

int main() {
    FuncPtr fp = greet;
    fp(3);
    return 0;
}
  

3. Typedef with Enum

#include <stdio.h>

typedef enum {
    RED, GREEN, BLUE
} Color;

int main() {
    Color c = GREEN;
    printf("Color value: %d\n", c);
    return 0;
}
  

4. Enum without Typedef

enum Color { RED, GREEN, BLUE };
enum Color c = RED;
  

5. Typedef with Structure

#include <stdio.h>

typedef struct {
    int x, y;
} Point;

int main() {
    Point p1 = {10, 20};
    printf("x = %d, y = %d\n", p1.x, p1.y);
    return 0;
}
  

6. Structure Without Typedef

struct Point {
    int x, y;
};

int main() {
    struct Point p = {10, 20};
}
  

7. Another Typedef Struct Example

#include <stdio.h>

typedef struct {
    int x, y;
} point;

int main() {
    point p;
    p.x = 3;
    p.y = 5;
    printf("%d and %d", p.x, p.y);
}
  

8. Typedef with Unsigned Int

#include <stdio.h>

typedef unsigned int unit;

int main() {
    unit a = 0;
    printf("The value of a is: %u", a);
}
  

Comments

πŸŒ™