C Programming for Beginners – Chapter 25 : Structures as function arguments in C

Free eBooks for Beginners

In C programming, structures can be used as arguments to functions. This allows you to pass a complex data type to a function, rather than passing individual pieces of information separately. In this article, we’ll explain what this means, why it’s useful, and how to use structures as function arguments in C.

When you pass a structure as an argument to a function, the entire structure is passed to the function as a single unit. This makes it easy for the function to access all of the data in the structure, without having to pass each piece of information separately. For example, consider the following code:

struct person { 
               char name[100]; 
               int age; 
               char address[200]; 
};

void print_person(struct person p) { 
                 printf("Name: %s\n", p.name); 
                 printf("Age: %d\n", p.age); 
                 printf("Address: %s\n", p.address); 
                }

int main() { 
            struct person person1; 
            strcpy(person1.name, "John Doe"); 
            person1.age = 30; 
            strcpy(person1.address, "123 Main St"); print_person(person1); return 0; }

In this example, the “print_person” function takes a single argument, which is a structure of type “person”. The function uses the structure argument to access the name, age, and address of the person, and prints those values to the screen.

Using structures as function arguments is useful because it makes it easy to pass complex data types to functions. For example, you can write a single function that takes a structure as an argument, and use that function to process data for many different structures in your program. This makes your code more modular and reusable, and makes it easier to maintain and update your code in the future.

In addition to passing structures by value, as shown in the example above, you can also pass structures by reference. This means that you pass a pointer to the structure to the function, rather than passing the structure itself. When you pass a structure by reference, the function can modify the original structure, and those changes will be reflected in the caller. For example, the following code passes a structure by reference:

void increase_age(struct person *p) { p->age++; }

int main() { 
            struct person person1; 
            
            strcpy(person1.name, "John Doe"); 
            person1.age = 30; 
            
            strcpy(person1.address, "123 Main St"); 
            increase_age(&person1); 
            print_person(person1); 
           
            return 0; 
}

In this example, the “increase_age” function takes a pointer to a structure of type “person”. The function uses the pointer to access the “age” field of the structure, and increments that value. When the function returns, the original structure has been modified, and the changes are reflected in the caller.

In conclusion, structures in C programming can be used as arguments to functions. This allows you to pass complex data types to functions, and makes your code more modular and reusable. Whether you pass structures by value or by reference, structures as function arguments are a powerful tool for writing efficient, maintainable, and modular C code.

C Programming for Beginners – Chapter 25 : Structures as function arguments in C

 

Loader Loading...
EAD Logo Taking too long?

Reload Reload document
| Open Open in new tab

Download PDF [573.68 KB]

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners