Working with Two-Dimensional Array at Runtime in C Programming

Bhanu Priya
Updated on 09-Mar-2021 08:40:45

1K+ Views

ProblemWrite a C program to calculate sum and product of all elements in two-dimensional array using run time compilation.SolutionRuntime compilation or initialization is also called as dynamic allocation. Allocation of memory at the time of execution (run time) is known as dynamic memory allocation.The functions calloc() and malloc() support allocating of dynamic memory.In this program, we will calculate the sum of all elements and product of all elements of two-dimensional array at run time.Logic for computing sum of all elements in 2D array −printf("Sum array is : "); for(i=0;i

Dynamic Memory Allocation in C Programming

Bhanu Priya
Updated on 09-Mar-2021 08:39:33

13K+ Views

Dynamic Memory AllocationAllocation of memory at the time of execution (run time) is known as dynamic memory allocation.The functions calloc() and malloc() support allocating of dynamic memory.Dynamic allocation of memory space is done by using these functions when value is returned by functions and assigned to pointer variables.In this case, variables get allocated only if your program unit gets active.It uses the data structure called heap for implementing dynamic allocation.There is memory reusability and memory can be freed when not required.It is more efficient.In this memory allocation scheme, execution is slower than static memory allocation.Here memory can be released at ... Read More

Static Memory Allocation in C Programming

Bhanu Priya
Updated on 09-Mar-2021 08:33:58

3K+ Views

Memory can be allocated in the following two ways −Static Memory AllocationStatic variable defines in one block of allocated space, of a fixed size. Once it is allocated, it can never be freed.Memory is allocated for the declared variable in the program.The address can be obtained by using ‘&’ operator and can be assigned to a pointer.The memory is allocated during compile time.It uses stack for maintaining the static allocation of memory.In this allocation, once the memory is allocated, the memory size cannot change.It is less efficient.The final size of a variable is decided before running the program, it will ... Read More

Assign Pointer to Function in C

Bhanu Priya
Updated on 09-Mar-2021 08:32:14

747 Views

Pointer to functionIt holds the base address of function definition in memory.Declarationdatatype (*pointername) ();The name of the function itself specifies the base address of the function. So, initialization is done using function name.For example, int (*p) (); p = display; //display () is a function that is defined.Example 1We shall see a program for calling a function using pointer to function −#include main (){    int (*p) (); //declaring pointer to function    clrscr ();    p = display;    *(p) (); //calling pointer to function    getch (); } display (){ //called function present at pointer location    printf(“Hello”); ... Read More

Finding Even and Odd Numbers Dynamically in C Language

Bhanu Priya
Updated on 09-Mar-2021 08:31:18

1K+ Views

ProblemTo compute sum of even numbers and odd numbers in a set of elements using dynamic memory allocation functions.SolutionIn this program, we are trying to find even and odd numbers in a set of numbers.The logic used to find even numbers in a set elements is given below −for(i=0;i

Dynamic Memory Allocation in C with Example

Bhanu Priya
Updated on 09-Mar-2021 08:30:19

13K+ Views

ProblemFind the sum of n numbers entered by user using dynamically allocated memory using C programming.SolutionThe Dynamic memory allocation enables the C programmers to allocate memory at runtime.The different functions that we used to allocate memory dynamically at run time are −malloc () − allocates a block of memory in bytes at runtime.calloc () − allocating continuous blocks of memory at run time.realloc () − used for reduce (or) extend the allocated memory.free () − deallocates previously allocated memory space.Following C program is to display the elements and calculate sum of n numbers.Using dynamic memory allocation functions, we are trying ... Read More

Post and Pre-Increment of Arrays in C Language

Bhanu Priya
Updated on 09-Mar-2021 08:28:40

5K+ Views

ProblemExplaining the array post and pre incremented concept with the help of C program.SolutionIncrement operator (++) −It is used to increment the value of a variable by 1There two types of increment operators − pre increment and post increment.Increment operator is placed before the operand in preincrement and the value is first incremented and then operation is performed on it.eg: z = ++a; a= a+1 z=aIncrement operator is placed after the operand in post increment and the value is incremented after the operation is performed.eg: z = a++; z=a a= a+1Let’s consider an example to access particular elements in memory ... Read More

Concept of Pointer Accessing in C Language

Bhanu Priya
Updated on 09-Mar-2021 08:27:22

4K+ Views

Pointer is a variable which stores the address of other variable.Pointer declaration, initialization and accessingConsider the following statement −int qty = 179;Declaring a pointerint *p;‘p’ is a pointer variable that holds the address of another integer variable.Initialization of a pointerAddress operator (&) is used to initialize a pointer variable.int qty = 175; int *p; p= &qty;Let’s consider an example how the pointer is useful in accessing the elements in an array of string.In this program, we are trying to access an element which is present at particular location. The location can be found by using an operation.By adding pre incremented ... Read More

Dynamic Memory Allocation in C Language

Bhanu Priya
Updated on 09-Mar-2021 08:25:51

4K+ Views

ProblemFind out the maximum and minimum from an array using dynamic memory allocation in C.SolutionThe Dynamic memory allocation enables the C programmers to allocate memory at runtime.The different functions that we used to allocate memory dynamically at run time are −The malloc () − allocates a block of memory in bytes at runtime.The calloc () − allocates continuous blocks of memory at runtime.The realloc () − used to reduce (or) extend the allocated memory.The free () − deallocates previously allocated memory space.Finding maximum and minimum number in an array using dynamic memory allocationThe logic for finding maximum element in an ... Read More

Matrix Row Sum and Column Sum Using C Program

Bhanu Priya
Updated on 09-Mar-2021 08:21:57

3K+ Views

ProblemLet’s write a C program to compute the row sum and column sum of a 5 x 5 array using run time compilation.SolutionIn this program, we are entering the values of array which is of size 5X5 matrix during runtime in the console, with the help of for loops we are trying to add rows and columns.Logic for doing row sum is given below −for(i=0;i

Advertisements