ProblemWhat do you mean by String to number and number to string conversion in C programming language?SolutionThere are two functions available for conversion. They are −sscanf() − convert string to numbersprintf () − used for converting number to stringString to number conversionWe can convert string to number using the sscanf() function −Syntaxsscanf (string name, “control string”, variable list)Example#include main (){ char a[20] = “02 01 2010”; int day, mon, yr; clrscr(); sscanf (a, “%d%d %d”, &day, &mon, &yr); printf ( “Day =%d”, day); printf ( “Month = %d”, mon); printf ( “Year = ... Read More
Arrays of pointers (to strings)Array of pointers is an array whose elements are pointers to the base address of the string.It is declared and initialized as follows −char *a[3 ] = {"one", "two", "three"}; //Here, a[0] is a ptr to the base add of the string "one" //a[1] is a ptr to the base add of the string "two" //a[2] is a ptr to the base add of the string "three"AdvantagesUnlink the two-dimensional array of characters. In (array of strings), in array of pointers to strings there is no fixed memory size for storage.The strings occupy as many bytes as ... Read More
Pointer is a variable which stores the address of other variable.Features of PointersFollowing are the features of pointers −Saves the memory spaceExecution time is faster because of direct access to memory location.The memory is accessed efficiently with the pointer i.e. dynamically memory is allocated and deallocated.Pointers are used with data structures.Here is an example for search demonstration −We can access and print a particular character in a string by using pointers.Following example shows how to access the elements using pointer −Example Live Demo#include int main(){ char array[5] = "Tutorial", *ptr, i, *ptr1; ptr = &array[1]; ptr1 = ptr ... Read More
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 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
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
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
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
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
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
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP