Programming goCareer

Friday, 21 July 2017

Pointer is an extremely powerful programming tool. A pointer is a variable which used to store the address of another variable of same data type. It means that if we want to store the address of integer type then pointer type must be of integer and so on.In C language, we have a special type of pointer named void (*), which contains the address of any type of variable. This pointer is also known as Generic Pointer.

Syntax :

Data_type *Variable_name ;

int *p ;

Astrik (*) sign denotes that p is pointer variable not a normal variable.

Key Points :

Pointer contains the the address of another variable.

* sign denotes the pointer variable. It shows the value of the variables that address stored for a pointer variable.

To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns the address of that variable.

Pointer contains 2 bytes of memory for 16 bit processor.

If a pointer is assigned to NULL, it means it is pointing to nothing.

Example :

#include <stdio.h>

int main()

{

int x;

// Prints address of x

printf("%p", &x);

return 0;

}

Output :

0x7ffe6ee753ee

Example 2:

#include <stdio.h>

int main()

{

// A normal integer variable

int Var = 10;

// A pointer variable that holds address of var.

int *ptr = &Var;

// This line prints value at address stored in ptr.

// Value stored is value of variable "var"

printf("Value of Var = %d\n", *ptr);

// The output of this line may be different in different

// runs even on same machine.

printf("Address of Var = %p\n", ptr);

// We can also use ptr as lvalue (Left hand

// side of assignment)

*ptr = 20; // Value at address is now 20

printf("After doing *ptr = 20, *ptr is %d\n", *ptr);

return 0;

}

Output :

Value of Var = 10

Address of Var = 0x7ffc23954c74

After doing *ptr = 20, *ptr is 20

Array of Pointer :

There may be a situation when we want to maintain an array, which can store pointers to an int or char or any other data type available. Following is the declaration of an array of pointers to an integer −

Thursday, 20 July 2017

A string is an array of characters. The end of the string is marked with a special character, thenull character, which is simply represented by '\0'. Strings are enclosed by double quotes whereas, character is enclosed by single quotes always.

Declaration :

Strings are array of char data type. It is declared as follow :

char s[5] ;

Initialization :

char message [20] = "Hello world !";

char message [20] = {'H', 'e', 'l', 'l', 'o', '\0'};

char message [] = "Hello!"

when we declare "char message[20]”, 20 bytes of memory space is allocated for the string value. And when we declare "char message[]”, memory space will be allocated as per the requirement during execution time of program.

String Handling Functions :

Sometimes, we need to manipulate string in our program according to our needs. For this C programming language provide us some in-built String Manipulation Functions which are available in <string.h> header file.

These functions are listed below :

strlen() :

strlen() function is used to count the number of characters in a string.

strcmp() function is used to compare two strings. If both strings are equal then function will return value zero(0). If first string is greater than second than function return value >0 otherwise <0. This function is case sensitive.

Wednesday, 19 July 2017

In C language, arrays are referred to as structured data types. An array is a collection of data that holds fixed number of values of same data type.Arrays store a fixed-size sequential collection of elements of the same type. In other words, if we want to group similar type of value in conjunctive memory location then we use Array.Points :

Array might be belonging to any of the data types

Array size must be a constant value.

Always, adjacent memory locations are used to store array elements in memory.

Array should be initialized with zero or null while declaring, if we don’t assign any values to array.

The size and type of arrays cannot be changed after its declaration.

Arrays are of two types:

One-dimensional arrays

Multidimensional arrays

One-dimensional Arrays :

Group of values in adjacent memory location in x or y direction is 1-D array.

Declaration of One-dimensional array :

data_type array_name[array_size];

For example :

float val[10];

Here, we declared an array, val of floating-point type and size 10. Meaning, it can hold 10 floating-point values.

Key notes:

Arrays have 0 as the first index. In this example, val[0]

If the size of an array is n, then last element will be (n-1). In this example, val[9]

Suppose the starting address of val[0] is 2124d. Then, the next address, val[1], will be 2128d, address of val[2] will be 2132d and so on. Because size of float data type is 4 bytes.

Initialization of 1-D Array :

After an array is declared, it’s elements must be initialized. In C programming an array can be initialized at either of the following stages:

At compile time

At run time

Compile time initialization :

Compile time initialization of array elements is same as normal variable initialization. The general form of initialization of array is :

type array-name[size] = { list of values };

The values in the list are separated by commas. For ex, the statement

int number[3] = { 0,5,4 };

will declare the variable’ number’ as an array of size 3 and will assign the values to each element. If the number of values in the list is less than the number of elements, then only that many elements will be initialized. The remaining elements will be set to zero automatically.

Remember, if we have more initializers than the declared size, the compiler will produce an error.

Example :

#include <stdio.h>

int main () {

int n[ 10 ] ;/* n is an array of 10 integers */

int i, j ;

/* initialize elements of array n to 0 */

for ( i = 0; i < 10; i++ ) {

n[ i ] = i + 100; /* set element at location i to i + 100 */

}

/* output each array element's value */

for (j = 0; j < 10; j++ ) {

printf("Element[%d] = %d\n", j, n[j] );

}

return 0;

}

Output :

Element[0] = 100

Element[1] = 101

Element[2] = 102

Element[3] = 103

Element[4] = 104

Element[5] = 105

Element[6] = 106

Element[7] = 107

Element[8] = 108

Element[9] = 109

Run time initialization :

An array can also be initialized at runtime using scanf() function. This approach is usually used for initializing large array, or to initialize array with user specified values.

Example :

#include<stdio.h>

int main()

{

int array[5],i;

printf("Enter 5 numbers to store them in array : \n");

for(i=0;i<5;i++)

{

scanf("%d",&array[i]);

}

printf("Element in the array are : \n \n");

for(i=0;i<5;i++)

{

printf("Element stored at a[%d] : %d \n",i,array[i]);

}

return 0;

}

Output :

Enter 5 elements in the array : 23 45 32 25 45

Elements in the array are :

Element stored at a[0] : 23

Element stored at a[0] : 45

Element stored at a[0] : 32

Element stored at a[0] : 25

Element stored at a[0] : 45

Multi dimension Array :

In C, we can define multidimensional arrays in simple words as array of arrays. Data in multidimensional arrays are stored in tabular form.

General form of declaring N-dimensional arrays:

data_type array_name[size1][size2]....[sizeN];

where,

data_type: Type of data to be stored in the array.

Here data_type is valid C/C++ data type

array_name: Name of the array

size1, size2,... ,sizeN: Sizes of the dimensions

Two dimensional Array :

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays.

Declaration :

type array-name[row-size][column-size]

int a[3][4];

Initialization :

int a[3][4] = {

{0, 1, 2, 3} , /* initializers for row indexed by 0 */

{4, 5, 6, 7} , /* initializers for row indexed by 1 */

{8, 9, 10, 11} /* initializers for row indexed by 2 */

};

int a[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};

Example :

#include <stdio.h>

int main () {

/* an array with 5 rows and 2 columns*/

int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};

int i, j;

/* output each array element's value */

for ( i = 0; i < 5; i++ ) {

for ( j = 0; j < 2; j++ ) {

printf("a[%d][%d] = %d\n", i,j, a[i][j] );

}

}

return 0;

}

Output :

a[0][0]: 0

a[0][1]: 0

a[1][0]: 1

a[1][1]: 2

a[2][0]: 2

a[2][1]: 4

a[3][0]: 3

a[3][1]: 6

a[4][0]: 4

a[4][1]: 8

Three dimensional Array :

Three-Dimensional array is same as that of Two-dimensional arrays. The difference is as the number of dimension increases so the number of nested braces will also increase.

Initialization of a three dimensional array :

You can initialize a three dimensional array in a similar way like a two dimensional array. Here's an example,

C programming has several in-built functions to perform input and output tasks.

Two commonly used functions for I/O (Input/Output) are printf() and scanf().

printf() and scanf() functions are inbuilt functions in C programming environment which are available in C library by default. These functions are declared and related with “stdio.h” which is a header file in C language.

printf() method in C :

printf() function sends formatted output to the standard output(screen). printf() is used to show any message or value of any data type.

We use printf() function with %d format specifier to display the value of an integer variable. Similarly %c is used to display character, %f for float variable, %s for string variable, %lf for double and %x for hexadecimal variable.