Skip to main content

Posts

Dynamic Memory Allocation in C

As you know, you have to declare the size of an array before you use it. Hence, the array you declared may be insufficient or more than required to hold data. To solve this issue, you can allocate memory dynamically. Dynamic memory management refers to manual memory management. This allows you to obtain more memory when required and release it when not necessary. There are 4 library functions defined under <stdlib.h> for dynamic memory allocation. Function Use of Function malloc() Allocates requested size of bytes and returns a pointer first byte of allocated space calloc() Allocates space for an array elements, initializes to zero and then returns a pointer to memory free() deallocate the previously allocated space realloc() Change the size of previously allocated space malloc() The name malloc stands for "memory allocation". The function malloc() reserves a block of memory of specified size and return a point

Storage classes in C, Scope and Life Time

In C variables are declared by the type of data they can hold. During the execution of the program, these variables may be stored in the registers of the CPU or in the primary memory of the computer. To indicate where the variables would be stored, how long they would exist, what would be their region of existence, and what would be the default values, C provides four storage class specifiers that can be used along with the data type specifiers in the declaration statement of a variable. These four storage class specifiers are  Automatic External Register Static Storage classes specify the scope, lifetime and binding of variables. To fully define a variable, one needs to mention not only its ‘type’ but also its storage class. A variable name identifies some physical location within computer memory, where a collection of bits are allocated for storing values of variable. Storage class tells us the following factors − Where the variable is stored (in memory or cpu register)? What will be

Bitwise Operators in C

Bit level operators make C useful for many low level applications. Bitwise operations allow one to read and manipulate bits in variables of certain types. The bit wise operators only work on int and char types. The following are the bitwise operators in C Operator Description ~ Bitwise Complement <<   Bitwise Shift Left >>   Bitwise Shift Right & Bitwise AND ^ Bitwise EX-OR | Bitwise OR Bitwise operations helps to work on Boolean variable, which needs only one bit.Set membership can be represented with Boolean variables ( 1 means element in the set 0 means not in the set) and the Bitwise-AND can be used to implement set-intersection and Bitwise-OR to implement set union. The bitwise AND(&) is true only if both bits are set. The bitwise OR(|) is false only when both the bits are false. The bitwise EX-OR operation returns 1 if the input bits are different. Th

Pointer to Functions

One of the powerful features of C is to define pointers to functions. Function pointers are pointers ie; variables, which point to the address of a function. Like other pointer variables, function pointers can be declared, assigned values and then used to access the function they point to. Function pointers are declared as follows return_type (* function_pointer_name) (argument_type1,argument_type2…); Example: int (*fp)(float,char) The function pointer fp points to a function that takes one float and one char arguments and returns an int value. Like other pointer variables, function pointers must be initialized prior to use. It is easy to assign the address of a function to a function pointer by using the name of the function. The following example illustrates the use of function pointer to call two different functions. int add(int a, int b) { return a+b; } int sub(int a, int b) { return a-b; } int main() { int (*fptr)(int,int); fptr=add; printf(“%d\n”,fptr(2,3)

Structure and Pointers

Structures can be created and accessed using pointers. A pointer variable of a structure can be created as below: struct name { member1; member2; ..... }; int main() { struct name *ptr; } Here, the pointer variable ptr of type struct name is created. Accessing structure's member through pointer A structure's member can be accessed through pointer in two ways: 1.Referencing pointer to another address to access memory. 2.Using dynamic memory allocation. 1. Referencing pointer to another address to access the memory Consider an example to access structure's member through pointer. #include <stdio.h> typedef struct person { int age; float weight; }; int main() { struct person *personPtr, person1; personPtr = &person1; // Referencing pointer to memory address of person1 printf("Enter Age: "); scanf(“%d”,&personPtr->age); or scanf("%d",&(*personPtr).age); printf("Enter Weight: "); scanf("%f",&pe

Recursion in C

Recursion is a method of solving problems that involves breaking a problem down into smaller and smaller sub problems until you get to a small enough problem that it can be solved trivially. Usually recursion involves a function calling itself. While it may not seem like much on the surface, recursion allows us to write elegant solutions to problems that may otherwise be very difficult to program. It is legal for one function to call another, and you have seen several examples of that. It is also legal for a function to call itself. It may not be obvious why that is a good thing, but it turns out to be one of the most magical and interesting things a program can do. A recursive function is one that calls itself directly or indirectly to solve a smaller version of its task until a final call which does not require a self call. What is needed for implementing recursion? Decomposition into smaller problem of same type. Recursive calls must diminish problem size. Necessity of base c