B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Mostly Asked C Programming Questions – Pointers and Structures | LiveCodeProgramming

Mostly Asked C Programming Questions – Pointers and Structures

Q1. Pointer declaration and useपॉइंटर घोषणा और उपयोग

Pointers are variables that store memory addresses. They are declared using the * operator. For example, int *p; declares a pointer to an integer. Pointers are used for dynamic memory, arrays, and passing by reference.

Example:

int a = 10;
int *p = &a;
printf("Value of a = %d", *p);

पॉइंटर्स ऐसे वेरिएबल होते हैं जो मेमोरी एड्रेस स्टोर करते हैं। इन्हें * ऑपरेटर से घोषित किया जाता है। उदाहरण के लिए, int *p; एक इंटीजर के लिए पॉइंटर घोषित करता है। पॉइंटर्स का उपयोग डायनामिक मेमोरी, एरे, और कॉल-बाय-रेफरेंस के लिए किया जाता है।

उदाहरण:

int a = 10;
int *p = &a;
printf("a का मान = %d", *p);

Q2. NULL pointerNULL पॉइंटर

A NULL pointer is a pointer that points to nothing (address 0). It is used to indicate that the pointer is not assigned any valid memory address yet.
Example: int *p = NULL;

NULL पॉइंटर ऐसा पॉइंटर होता है जो किसी भी वैध मेमोरी एड्रेस की ओर इशारा नहीं करता (एड्रेस 0)। इसका उपयोग यह दिखाने के लिए किया जाता है कि पॉइंटर अभी तक किसी वैध मेमोरी एड्रेस से लिंक नहीं हुआ है।
उदाहरण: int *p = NULL;

Q3. & and * in pointers& और * पॉइंटर्स में

The & operator returns the address of a variable (address-of operator). The * operator is the dereference operator, which accesses the value stored at the address the pointer points to.

Example:

int a = 5;
int *p = &a;
printf("Address: %p", p);
printf("Value: %d", *p);

& ऑपरेटर किसी वेरिएबल का एड्रेस लौटाता है (address-of operator)। * ऑपरेटर डिरेफरेंस ऑपरेटर है, जो पॉइंटर द्वारा पॉइंट किए गए एड्रेस पर स्टोर वैल्यू को एक्सेस करता है।

उदाहरण:

int a = 5;
int *p = &a;
printf("एड्रेस: %p", p);
printf("मान: %d", *p);

Q4. Arrays and pointers relationshipएरे और पॉइंटर्स का संबंध

Array names behave like pointers to their first element. You can use pointer arithmetic to iterate through array elements.

Example:

int arr[3] = {10, 20, 30};
int *p = arr;
printf("First element: %d", *p);
p++;
printf("Second element: %d", *p);

एरे नाम अपने पहले एलिमेंट के लिए पॉइंटर की तरह व्यवहार करते हैं। आप पॉइंटर अंकगणित का उपयोग करके एरे के तत्वों को एक्सेस कर सकते हैं।

उदाहरण:

int arr[3] = {10, 20, 30};
int *p = arr;
printf("पहला तत्व: %d", *p);
p++;
printf("दूसरा तत्व: %d", *p);

Q5. struct vs unionstruct बनाम union

A struct groups variables of different types under one name, allocating memory for all members.
A union allows storing different data types in the same memory location, sharing memory among members.

Example:

struct Example {
  int i;
  float f;
};

union Example {
  int i;
  float f;
};

struct विभिन्न प्रकार के वेरिएबल्स को एक नाम के तहत समूहित करता है और सभी मेंबर के लिए मेमोरी अलॉट करता है।
union विभिन्न डेटा प्रकारों को एक ही मेमोरी लोकेशन में स्टोर करने की अनुमति देता है, जिससे मेमोरी साझा होती है।

उदाहरण:

struct Example {
  int i;
  float f;
};

union Example {
  int i;
  float f;
};

Q6. Structure declaration and accessस्ट्रक्चर घोषणा और एक्सेस

Structures are declared using the keyword struct. Members are accessed using the dot (.) operator.

Example:

struct Person {
  char name[20];
  int age;
};

struct Person p1;
strcpy(p1.name, "John");
p1.age = 30;

Structures को struct कीवर्ड से घोषित किया जाता है। मेंबर्स तक पहुंचने के लिए डॉट (.) ऑपरेटर का उपयोग किया जाता है।

उदाहरण:

struct Person {
  char name[20];
  int age;
};

struct Person p1;
strcpy(p1.name, "John");
p1.age = 30;

Q7. Nested structureनेस्टेड स्ट्रक्चर

Structures can contain other structures as members (nested). This helps organize complex data.

Example:

struct Date {
  int day, month, year;
};

struct Employee {
  char name[30];
  struct Date dob;
};

Structures के अंदर अन्य structures को मेंबर्स के रूप में रखा जा सकता है (nested)। यह जटिल डेटा को व्यवस्थित करने में मदद करता है।

उदाहरण:

struct Date {
  int day, month, year;
};

struct Employee {
  char name[30];
  struct Date dob;
};

Q8. Memory in union vs structureunion बनाम structure में मेमोरी

In a structure, memory is allocated for all members separately, total size is the sum of sizes.
In a union, all members share the same memory location, so size equals the largest member's size.

Structure में, सभी मेंबर्स के लिए अलग-अलग मेमोरी अलॉट होती है, कुल आकार सभी का योग होता है।
Union में, सभी मेंबर्स एक ही मेमोरी लोकेशन साझा करते हैं, इसलिए आकार सबसे बड़े मेंबर के आकार के बराबर होता है।

Q9. Use of typedeftypedef का उपयोग

typedef creates a new name (alias) for an existing type, making code cleaner and easier to read.

Example:

typedef struct {
  int x, y;
} Point;

Point p1;

typedef मौजूदा प्रकार के लिए नया नाम (एलियास) बनाता है, जिससे कोड साफ़ और पढ़ने में आसान हो जाता है।

उदाहरण:

typedef struct {
  int x, y;
} Point;

Point p1;

Q10. Pointer to structureस्ट्रक्चर के लिए पॉइंटर

A pointer to a structure points to the memory address of a struct variable. Members are accessed using the arrow (->) operator.

Example:

struct Person {
  char name[20];
  int age;
};

struct Person p1;
struct Person *ptr = &p1;
ptr->age = 25;

Structure का पॉइंटर struct वेरिएबल के मेमोरी एड्रेस की ओर इशारा करता है। मेंबर्स तक पहुंचने के लिए arrow (->) ऑपरेटर का उपयोग किया जाता है।

उदाहरण:

struct Person {
  char name[20];
  int age;
};

struct Person p1;
struct Person *ptr = &p1;
ptr->age = 25;