0% found this document useful (0 votes)
4 views2 pages

Ans 3

Great

Uploaded by

nitinchavda2615
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Ans 3

Great

Uploaded by

nitinchavda2615
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Answer 3

Yes, we can use structures with functions in C. Structures can be passed to


functions **by value** or **by reference (using pointers)**. Here's a concise
explanation and example:

### Passing Structure to Function

#### 1. **By Value**


A copy of the structure is passed to the function. Changes inside the function
don't affect the original structure.

#include <stdio.h>

struct Student {
int rollNumber;
float marks;
};

// Function to display structure details


void display(struct Student s) {
printf("Roll Number: %d\n", s.rollNumber);
printf("Marks: %.2f\n", s.marks);
}

int main() {
struct Student s1 = {101, 95.5};
display(s1); // Passing structure by value
return 0;
}
```

---

#### 2. **By Reference**


A pointer to the structure is passed, allowing the function to modify the original
structure.

#include <stdio.h>

struct Student {
int rollNumber;
float marks;
};

// Function to modify structure


void updateMarks(struct Student *s, float newMarks) {
s->marks = newMarks; // Access using pointer
}

int main() {
struct Student s1 = {101, 95.5};
printf("Before Update: %.2f\n", s1.marks);
updateMarks(&s1, 98.0); // Passing structure by reference
printf("After Update: %.2f\n", s1.marks);
return 0;
}

You might also like