Skip to main content

Posts

Showing posts from December, 2020

Arrays and static Memory allocation in C

In this post we will be talking about array, their usage and importance in C. Array Array in simple world is a collection/container of elements of  same type. Array of any type is possible but the elements in array cannot be of multiple types. We can have array of int type. But not a array with 5 element of int and other element of other types . Why Array? Imagine a problem where you want to input the marks of 10 student in a subject  from the user and display the marks in descending number. Our major problem above will be to store the number input by the user. Here we have two options : Either construct 10 variables for storing marks of 10 student .(A trash Idea) Or Construct a container (Array of integers in this case) which can store all the marks of student . First Glance of an Array As mentioned earlier array is a nothing else than a collection for us. int   marks [ 10 ]  =  { ' 1 ' , ' 2 ' , ' 3 ' , ' 4 ' , ' 5 ' }; This is what an array lo

What is Dynamic Memory Allocation in C

Under stdlib header C provides us 4 four special function which helps in allocating memory dynamically and freeing it. malloc() calloc() realloc() free() The first three function's are used in allocation of memory while the last one frees the allocated memory. The Malloc function Syntax malloc ( number_of_bytes ); The malloc function  allocates contiguous memory location for the required   number_of_bytes, returns a void pointer pointing to the base address of that allocated memory. A malloc returns a void pointer,why?because malloc does not know for what we allocated the memory ( malloc does not know whether we are allocating memory for ints , floats or chars ). Usage example int   * a = ( int * )  malloc (5 *sizeof ( int )); The (int*) is done for typecasting, it converts the void pointer returned by malloc function to a integer pointer (int*) . We are then assigning the returned(and type casted) pointer to a integer point a. So a is now pointing to the base address of allocated

What is Operator Overloading in c++

  Who doesnot love syntactic sugar ? After all they make our code look more beautiful. So let us discuss how to  overload operators in C++ How to Overload Operators in C++ What if I tell you ,You can use most of the operator in C++ not only with built in type but with the  type you defined yourself (Classes are user defined type) .Yes you got it right , operator overloading lets you define the meaning of  an operator when used to operands of user defined type(classes) Overloaded operator are nothing else than a function (however a special one) with a special keyword operator followed by the symbol for the operator to be overloaded. As I already mentioned that overloaded opeartors are a function so they definetly have a return type,parameter(s) and a body. It should look something like this: returnType operator Symbol(){} As an example:   int   operator  +(){ // body } Let us now overload   plus operator (+) for our class . 1.# include < iostream > 2.using   namespace  std; 3. 4