Pointers to structures may be declared as we declare pointers to any other data type. Like arrays and functions, the name of structure carries the address of the structure where the values of its various members are stored. Therefore, the pointer may be initialized by the name of structure which is a constant pointer to the structure. Program illustrates the declaration and use of pointer to a structure. [Read more…] about C Program to Pointers to structures
Typedef with Nested Structures in C
We have used the declaration of nested structures on the parent structure in the same declaration. The structures may also be declared separately and included in the parent structure. The innermost structure should be declared first, then the next enveloping structure and then the next enveloping structure. The code is illustrated in Program. [Read more…] about Typedef with Nested Structures in C
Typedef in Structure Declaration in C
In this method, the declaration starts with typedef followed by key word struct. The data members of the structure are placed between a pair of curly braces after struct. The type name is placed after the closing right brace (}). This is explained in the following example: [Read more…] about Typedef in Structure Declaration in C
C Program for memchr () and memcmp () functions.
memchr (): The function searches for first occurrence of c through the first n characters of the string pointed to by S. If successful it returns pointer to c in the string. If not successful, it returns NULL or 0. [Read more…] about C Program for memchr () and memcmp () functions.
Arrays of Strings in C
A string is defined to be an array of characters. If it is desired to have an array of strings such as a list of names, it becomes similar to an array whose elements are also arrays. Therefore, an array of strings is a two-dimensional array; it may be declared as below. [Read more…] about Arrays of Strings in C
Strings in C
In C language, a string is defined as a variable length array of characters terminated by the null character (‘\O’). Letters such as A, b, C, etc., digits such as 1, 2, 3, etc., and special symbols such as +,- ,*, [] , () , etc., except a few control characters, may be included in a string. The various functions of C Standard Library for manipulation of strings are contained in header file <string.h>. [Read more…] about Strings in C
Passing arguments by value and by Pointers in C
Passing Arguments by Value
The arguments to a function may be passed on by value or by pointers. In the first case the copies of values of parameters are passed on to the function. The function can only manipulate these copies. So, the original values of parameters are not affected. Their values cannot be changed by the function because function does not know where the parameters are stored in the memory. For example, if the function simply swapped the values of its parameters, it is only swapping the copies of the values of parameters. The original values of parameters are not affected. In Program, the arguments are passed on to the function by values. These values are changed by the function and then an expression is evaluated. The function may be called any numbers of times, the output of the program as well as the data fed to function do not change. This is the benefit of passing the arguments by value. [Read more…] about Passing arguments by value and by Pointers in C
Pointers to Strings in C
Strings are character arrays terminated by null character. Pointer to a single string may be declared in the same way as the pointer to a one-dimensional array. [Read more…] about Pointers to Strings in C
C Program manipulating array elements with pointers
An individual element of an array may be regarded as a variable of the type declared in the declaration of the array. All the operators that can be applied to a variable of that type are applicable to the elements of array as well. Program, the array elements are subjected to arithmetic operations with the help of pointers. [Read more…] about C Program manipulating array elements with pointers
C Program pointers for traversing an array
One-Dimensional Array with Pointer in C
The name or identifier of an array is itself a constant pointer to the array. Its value is the address of the first element of the array. Thus, a pointer to an array may be declared and assigned as shown below. [Read more…] about One-Dimensional Array with Pointer in C
C Program to computes the area and perimeter of a rectangle using pointers.
C Program to size of pointers to all data types is same
The values of pointer variables are unsigned integer numbers which are addresses of other variables. An unsigned integer is allocated 4 bytes of memory for its storage on a typical 32-bit system. Thus, pointers to all types of data occupy the same size of memory because the value of a pointer is the memory address – an unsigned integer. However, the variables to which the pointers point to, of course, occupy different sizes of memory blocks according to their types. [Read more…] about C Program to size of pointers to all data types is same
C Program to use typedef in pointer
The typedef may be used in declaration of a number of pointers of the same type. It does not change the type, it only creates a synonym, i.e., another name for the same type as illustrated below. [Read more…] about C Program to use typedef in pointer
Type Casting Of Pointers in C
We saw that pointer values may be assigned to pointers of same type. However, pointers may be type cast from one type to another type. In the following code lines, A is an int type variable, D is variable of type double, and ch is a variable of type char. Pa is declared as a pointer to int variables, Pd is declared as a pointer to double type variables, and Pc is declared as pointer to character type variables. Pa is assigned the value &A. [Read more…] about Type Casting Of Pointers in C
Declaration and Initialization of Pointers in C
The declaration of a pointer variable in C, comprises the type of pointer followed by indirection operator (*) which is followed by an identifier for the pointer. The declaration is done as follows: [Read more…] about Declaration and Initialization of Pointers in C
Advantages of using pointers in C
Except a few, most of the programs in C may be written with or without pointers. Then the question arises “Why use pointers if you can do without them?” Pointers are considered to be useful tools in programming because of the following reasons: [Read more…] about Advantages of using pointers in C
Three-Dimensional Arrays in C
A three-dimensional array is that array whose elements are two-dimensional arrays. In practice, it may be considered to be an array of matrices. A three-dimensional array with int elements may be declared as below. [Read more…] about Three-Dimensional Arrays in C
C Program passing of more than one arrays to a function
The procedure of passing more than one arrays to a function is similar to the case of passing a single array. This is illustrated in Program. [Read more…] about C Program passing of more than one arrays to a function
C Program sorting of an int array using Insertion Method
Program sorts the integers in ascending order by using insertion method. The process of sorting starts with picking the second element which is arranged with respect to the first in the specified order (ascending or descending order). [Read more…] about C Program sorting of an int array using Insertion Method
C Program sorting of an int array using Bubble Sort
Suppose, we want to sort an array in ascending order. The elements with higher values will move back, while elements with smaller values will move to the front; the smallest element will become the 0th element and the largest will be placed at the end. The mechanism of sorting is explained below. [Read more…] about C Program sorting of an int array using Bubble Sort
C Program determination of arithmetic mean and standard deviation
Each element of an array is a variable of the type declared for the array. Thus, if an array is declared as int Array [n],all its n elements are of type int, and all the operations that can be done on variables of type int may be carried out on the elements of this array. The same holds for arrays of types float, double, and char. In Program integer array elements are subjected to arithmetic operations. [Read more…] about C Program determination of arithmetic mean and standard deviation
C Program finding of addresses of an array and its elements
When a program containing an array of size n is compiled, the compiler allocates n blocks of memory for the array for storing the values of its elements. The size of each block depends on the data type of the array. For example, for the array AR [ ] defined below, five blocks of memory are allocated and each block is of the size of the memory block for an integer (i.e., 4 bytes on a 32-bit system). [Read more…] about C Program finding of addresses of an array and its elements
C Program Maximum/Minimum Value in an Array
The maximum or minimum value may be determined by comparing the values of array elements. Let us assume that the maximum value is represented by ‘max’. The value of the first element of the array is assigned to max. [Read more…] about C Program Maximum/Minimum Value in an Array
C Program binary search of an array for a value
In case of large arrays, the method of comparing every array element with key as described above is rather inefficient. However, if an array is a sorted one, the search process can be considerably shortened by using binary search method. For the application of binary search the array should be first sorted. [Read more…] about C Program binary search of an array for a value
C Program sequential search of a value in an array
Searching an array for a value is the most common occurrence in programming as well as in real life. Often it is required to search a name in a list of names or search a roll number in a list of roll numbers. [Read more…] about C Program sequential search of a value in an array
C Program swapping of elements of two arrays
Any operation on an array has to be carried out element by element. It cannot be performed on the array as a whole. Therefore, in swapping also, an element of one array is swapped with an element of another array. The two may not have the same index value if you are not dealing with vectors and matrices. [Read more…] about C Program swapping of elements of two arrays
C Program input/output of an array with the help of a for loop
An array is similar to a list in which the objects are of the same type and stored in sequential memory blocks; this is the only relationship between the elements of an array. The input/output of values of elements of an array cannot be done as whole of array, but is carried out element by element. This may be done by accessing each element by its index value. Therefore, for large arrays, either a for loop or a while loop may be used for accessing each element for its input/output. For example, if elements of an array Array [4] having 4 elements are to be processed for input /output by user of a program, the program code may be written as follows: [Read more…] about C Program input/output of an array with the help of a for loop
Inline Functions in C
The qualifier inline has been introduced in C-99 in order to achieve better compatibility between C and C++. If a small function needs to be called a large number of times in a program, the overburden of function call can make the program inefficient. Also, if the function code is put in the listing of the program wherever it is needed, it will not only make the program lengthy but also clumsy. With qualifier inline we need to define the function only once and the compiler substitutes the code wherever the function is called in the program. This eliminates the multiple function call as well as the drudgery of typing the function code a large number of times in the program. Thus, the qualifier inline makes a program efficient and also lessens the work of the programmer. An illustration of its application is illustrated below. [Read more…] about Inline Functions in C
C Program use of time() as seed number
In many cases and particularly in computer games, a seed number cannot be provided on every event. We need some function whose return value is always changing. Time is always changing; so, we make use of function time() defined in the header file <time.h>. [Read more…] about C Program use of time() as seed number