B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP

C Pointers

Pointers in C store the memory address of variables. They allow direct access and manipulation of memory.

C में पॉइंटर वैरिएबल्स के मेमोरी एड्रेस को स्टोर करते हैं। ये मेमोरी को सीधे एक्सेस और मैनीपुलेट करने देते हैं।

Example 1: Pointer Declaration and Usage
#include <stdio.h>

int main() {
  int var = 10;
  int *ptr = &var;
  printf("Value of var: %d\n", var);
  printf("Address of var: %p\n", &var);
  printf("Pointer ptr points to: %p\n", ptr);
  printf("Value pointed by ptr: %d\n", *ptr);
  return 0;
}

Output:

Value of var: 10
Address of var: 0x7ffee4c3f79c
Pointer ptr points to: 0x7ffee4c3f79c
Value pointed by ptr: 10

Pointer stores address of variable and accesses its value.

पॉइंटर वैरिएबल का पता स्टोर करता है और उसकी वैल्यू को एक्सेस करता है।

Example 2: Pointer Arithmetic
#include <stdio.h>

int main() {
  int arr[3] = {10, 20, 30};
  int *ptr = arr;

  printf("First element: %d\n", *ptr);
  ptr++;
  printf("Second element: %d\n", *ptr);
  ptr++;
  printf("Third element: %d\n", *ptr);
  return 0;
}

Output:

First element: 10
Second element: 20
Third element: 30

Pointer arithmetic used to traverse array elements.

पॉइंटर अरिथमेटिक से एरे के एलिमेंट्स को एक्सेस करना।

Example 3: Pointer to Pointer
#include <stdio.h>

int main() {
  int var = 50;
  int *ptr = &var;
  int **pptr = &ptr;

  printf("Value: %d\n", var);
  printf("Value using *ptr: %d\n", *ptr);
  printf("Value using **pptr: %d\n", **pptr);
  return 0;
}

Output:

Value: 50
Value using *ptr: 50
Value using **pptr: 50

Pointer to pointer stores address of another pointer.

पॉइंटर टू पॉइंटर एक दूसरे पॉइंटर का पता स्टोर करता है।

Example 4: Passing Pointer to Function
#include <stdio.h>

void increment(int *p) {
  (*p)++;
}

int main() {
  int x = 10;
  increment(&x);
  printf("Value after increment: %d\n", x);
  return 0;
}

Output:

Value after increment: 11

Passing pointer to function to modify variable.

फंक्शन में पॉइंटर पास करके वैरिएबल को मॉडिफाई करना।

Example 5: Dynamic Memory Allocation
#include <stdio.h>
#include <stdlib.h>

int main() {
  int *ptr = (int*) malloc(sizeof(int));
  if(ptr == NULL) {
    printf("Memory allocation failed\n");
    return 1;
  }
  *ptr = 100;
  printf("Value at allocated memory: %d\n", *ptr);
  free(ptr);
  return 0;
}

Output:

Value at allocated memory: 100

Dynamically allocated memory accessed using pointer.

डायनामिकली मेमोरी एलोकेट कर पॉइंटर से एक्सेस करना।