How to learn constructor – in C
In C, a constructor is not a built-in feature like in C++ or other object-oriented languages. However, you can simulate constructor behavior using functions, typically by initializing variables or allocating memory when creating an object or structure.
For example, you can create an initialization function for a structure in C to mimic a constructor:
c
#include <stdio.h> #include <string.h> // Define a structure struct Person { char name[50]; int age; }; // Simulate constructor using a function void initPerson(struct Person *p, const char *name, int age) { strcpy(p->name, name); p->age = age; } int main() { struct Person person; // Call the initialization function (constructor) initPerson(&person, "Raghu", 30); // Display the initialized values printf("Name: %s\n", person.name); printf("Age: %d\n", person.age); return 0; }
In this example, the initPerson
function acts like a constructor by initializing the fields of the Person
structure.
c
#include <stdio.h> #include <string.h>
#include <stdio.h>
: This includes the Standard Input/Output library, which provides functions likeprintf()
for printing to the console.#include <string.h>
: This includes the string handling library, which allows using functions likestrcpy()
to copy strings.
c
struct Person { char name[50]; int age; };
struct Person
: This defines a structure (which is a collection of different types of data grouped together) namedPerson
.char name[50];
: A character array (string) is used to store the name of the person. It can hold up to 49 characters (with 1 extra for the null terminator\0
).int age;
: An integer to store the age of the person.
c
void initPerson(struct Person *p, const char *name, int age) { strcpy(p->name, name); p->age = age; }
void initPerson
: This declares a function initPerson
that does not return any value (void
).
struct Person *p
: The function takes a pointer to a Person
structure as its first argument, allowing the function to modify the actual Person
object passed to it.
const char *name
: This is a constant pointer to a character array (string). It means the function can read from the name
string but cannot modify it.
int age
: This is an integer argument to pass the age of the person.
strcpy(p->name, name);
: This uses the strcpy()
function from <string.h>
to copy the string name
into the name
field of the Person
structure.
The ->
operator is used because p
is a pointer, and this is how you access the structure’s fields via a pointer.
p->age = age;
: This directly assigns the integer age
to the age
field of the Person
structure.
c
int main() { struct Person person;
int main()
: This is the entry point of the program where execution starts.
struct Person person;
: This declares a variable person
of type struct Person
. Memory is allocated for this structure to hold the name
and age
.
c
initPerson(&person, "Raghu", 30);
initPerson(&person, "John Doe", 30);
: This calls the initPerson
function and passes a pointer to person
(using the &
address-of operator), the string "John Doe"
, and the integer 30
. The function initializes the name
and age
fields of the person
structure.
c
printf("Name: %s\n", person.name); printf("Age: %d\n", person.age);
printf("Name: %s\n", person.name);
: This prints the name
field of the person
structure using the %s
format specifier (which is used for strings).
printf("Age: %d\n", person.age);
: This prints the age
field of the person
structure using the %d
format specifier (which is used for integers).
c
return 0;
return 0;
: This signals that the program has successfully completed. The return value of 0
typically indicates success.
Note: Overall, this program defines a Person structure, uses a function (initPerson) to simulate a constructor that initializes the structure’s fields, and prints the values.
Popular post
-
Eclipse IDE – Create New Java Project.
Opening the New Java Project…
-
How to start the project in android studio
Android Studio Open the Android…
-
How to use ACOSH function in excel
The ACOSH function returns the…
-
Complete Header tags in html – easy to learn
H tags can be used…
-
Best features in Python programme – easy to learn
Python is the most widely…