An array can be defined as the collection of variables of the same type that are referenced by a common name. Arrays are of two types namely one dimensional arrays and multi dimensional arrays. One dimensional array consists of finite homogenous elements whereas a multi dimensional array is composed of elements each of which is itself an array. Arrays refer to a named list of a finite number n of similar data elements. Each of the data elements can be referenced respectively by a set of consecutive numbers, usually 0, 1, 2, 3, 4,….., n. the simplest form of a multi dimensional array is the two dimensional array.
You can declare a two dimensional array as follows:
Type array name [ rows ] [ columns ];
Where type is the base data type of the array having name name, rows, the first index, refers to the number of rows in the array and columns, the second index refers to the number of columns in t5he array. For example:
Int sales [ 5 ] [ 10 ] ;
The general form of array initialization is shown below:
Type array name [ size N ] = { value list } ;
The value list is a comma separated list of array elements values. The element’s values in the value list must have the same data type as that of the base type of the array.
Int days [ 5 ] = { 1, 2, 3, 4, 5 } ;
Character arrays can also be initialized as shown below:
Char string [ 10 ] = { ‘c’ , ‘a’ , ‘t’ ‘\0’ } ;
Multi-dimensional arrays can also be initialized in the same way as simple dimensions one. For example:
Int abc [ 3 ] [ 2 ] = { 1, 1,
2, 2,
3, 3 } ;
This can be done it the other way also :
Int abc [ 3 ] [ 2 ] = { 1, 1, 2, 2, 3, 3 } ;
A multi dimensional array of strings can be initialized as shown below:
Type array name [ size N ] = { value list } ;
Here type declares the base type of the array, the array name specifies the name with which the array will be referenced and size defines how many elements the array will hold. The size must be an integer value or integer constant without any sign. The value list is a comma separated list of array’s elements values. The element values in the value list must have the same data type as that of the base type of the array.
Int days [ 5 ] = { 1, 2, 3, 4, 5 } ;
Character arrays can also be initialized as shown below:
Char string [ 10 ] = { ‘c’ , ‘a’ , ‘t’ , ‘s’ , ‘\0’ } ;
Multi dimensional arrays are also initialized in the same as the single dimensional one. For example:
Int cube [ 3 ] [ 2 ] = { 1, 1,
2, 2,
3, 3 } ;
This can be done in the other way also:
Int abc [ 3 ] [ 2 ] = { 1, 1, 2, 2, 3, 3 } ;
A multi dimensional array of strings can be initialized as shown below:
Char abc [ 3 ] [ 2 ] = { “Sunday” , “Monday” } ;
C allows you to skip the size of the array in an array initialization statement. This is called unsized array initialization. C allows arrays of more than 2 dimensions. The exact limit of dimensions is determined by the compiler we are using.
Monday, October 10, 2011
Some details about Multi dimensional arrays in C...
Posted by
Sunflower
at
10/10/2011 04:44:00 PM
0
comments
Labels: Array, Arrays, C, C Language, Characters, Columns, Constants, Data, Data types, Dimensions, Initialization, Integer, Languages, Multi dimensional arrays, Rows, Strings, Types, Values, Variables
![]() | Subscribe by Email |
|
Wednesday, October 5, 2011
Some details about Pointers to Arrays in C
A pointer is a variable that holds a memory address, usually of another variable in memory. The pointers are one of the most powerful and strongest features of C language. The correct understanding and use of pointers is critical to successful programming in C. pointer’s support C’s dynamic memory allocation routines. Pointers provide the means through which the memory location of a variable can be directly accessed and hence can be manipulated in the required way. Lastly, pointers can improve the efficiency of certain routines. Arrays and pointers are very loosely linked. C treats the name of array as if it were a pointer.
Consider the following code snippet:
Int *a ; // a is a pointer to an integer
Int age [10] ; //age is an array holding ten integers
For (int I = 0; I < 10 ; I ++)
a = age ; // makes a to point to the location where age points to. Age is a pointer pointing to age [0].
.
.
.
In the above code a is a pointer and age is an array holding 10 integers. The pointer a is made to point where age is pointing to. Since the name of an array is a pointer to its first element, the array name + 1 gives the address of the second element of the array, array name + 2 gives the address of the 3rd element, and so forth.
Pointers also may be arrayed like any other data type. To declare an array holding 10 integer pointers, the declaration would be as follows:
Int *ip [10] ; // array of 10 int pointers
After this declaration, contiguous memory would be allocated for 10 pointers that can point to integers. Now each of the pointers, the elements of pointer array, may be initialized. We can use the following statement:
Ip [3] = &a ;
To find the value of a, you can use the below given statement:
*ip [3] ;
The name of an array is actually a pointer to the first element of the array, the same holds true for the array of pointers also. Most often, an operation is carried on successive elements of an array. Using a loop for it and using the array elements indices. Consider the following code fragment that initializes an array to 0:
Const int abc = 20 ;
Int arr [ abc ] ;
For ( int I = 0 ; I < abc ; i++ )
Arr [ I ] = 0 ;
To execute the above code snippet, the compiler computes the address of array [ I ] every time by multiplying the index I by the size of an array element. That is, the compiler performs the multiplication for each element. A faster alternative would be to use a pointer as shown below:
Const int abc = 20 ;
Int arr [ abc ] ;
Int * arr2 ;
For ( arr2 = arr ; arr2 < &arr [ abc ] ; arr2++ )
*arr2 = 0;
Now the compiler only needs to evaluate a subscript once, when setting up the loop, and so saves 19 multiplication operations. So it is faster to use an element pointer than an index when you need to scan the arrays in a program. Pointers in c are defined by their data type and values. The data type determines the increment or decrements of the pointer value. The value is the address of the memory location to which the pointer is pointing. If you are using array notation, you don’t need to pass the dimensions.
Posted by
Sunflower
at
10/05/2011 07:45:00 PM
0
comments
Labels: Address, Arrays, C Language, Code, Data, Efficiency, Function, Integer, Memory, Pointers, Programming, Routines, Structures, Variables
![]() | Subscribe by Email |
|
Thursday, September 15, 2011
Some details about Pointers and Structures in C...
A pointer can be defined as a variable pointing to another variable or a variable storing the location or address of another variable.Pointers can be used to point to any data type.There are object pointers, function pointers, structure pointers and so on. Like any other pointers, structure pointers are also a very useful tool in C programming.Pointer structures are easy to declare and declared similarly like any other kind of pointer by putting a “*” sign before the name of the structure. See the example below:
Struct address *a1, *a2;
a1 = &b2;
a2 = &b3;
where b2 and b3 are the actual structure address variables. Using the below given assignment statement you can copy the details of structure pointed by a2 to a1:
*a1 = *a2;
Any member of a structure can be accessed using a pointer in the following way:
A->b
Here A is a pointer pointing to a structure and b is a data member or a member function of the structure being pointed. And there’s another way to access a member of a structure which has been given below:
(*A).b;
Both the statements are equivalent. Use of structure pointers is very common and useful.Be careful while dealing with structure pointers like any other normal pointer.The precedence of operators matters a lot. Be careful with the precedence of operators while programming in C.If we enclose *A is parentheses, an error will be generated and the code will not be compiled since the “.” Operator has got a high precedence than the”*” operator.Using so many parentheses can be quite a tedious job so, C allows us to use a shorthand notation to reduce the bombastic jargon. ” (*A).” can also be written as given below:
A ->
This is equivalent to (*A). but takes less characters. We can create pointers to structure arrays. A lot of space can be saved if we declare an array of pointers instead of an array to structures. In this only one structure is created and subsequently values are entered and disposed. Even structures can contain pointers as shown below:
Typedef struct
{
Char a[10];
Char *b;
} c;
c d;
char e[10];
gets(d.a,10);
d.b = (char *) malloc (sizeof (char[strlen(e)+ 1]));
strcpy(d.b, e);
This method is implemented when only a few records are required to be stored. When the size of the structure is large, it becomes difficult to pass and return the structures to the functions. To overcome this problem we can pass structure pointers to the functions. These structures can be accessed indirectly via pointers. Given below is a small code to illustrate the passing of structure pointers to the function and accessing them:
struct product
{
Int pno;
Float price;
};
Void inputpno (struct product *pnoptr);
Void outputpno (struct product *pnoptr);
Void main()
{
Struct product item;
Printf(“product details\n\n”);
Inputpno (&item);
Outputpno (&item);
}
Void outputpno (struct product *pnoptr)
{
Printf( “product no. = %d, price = %5.2f \n”, ( *pnoptr). Pno, (*pnoptr).price);
}
Void inputpno (struct product *pnoptr)
{
Int x;
Float y;
Struct product pno;
Printf( “product number: “);
Scanf( “%d”, &x);
( *pnoptr).pno = x;
Printf ( “price of the product: “);
Scanf( “%f”, &y);
( *pnoptr). Price = y;
}
In this program code, the prototypes, function calls and definitions have been changed in order to work with structure pointers. Dereferencing of a structure pointer is very common in programs. “->” (arrow) is an operator that is provided by C for accessing the member function of a structure. The 2 statements given below are equivalent:
Pnoptr -> pno;
(*pnoptr).pno;
Posted by
Sunflower
at
9/15/2011 08:44:00 PM
0
comments
Labels: Arrays, C, C Language, Code, Compilation, Definitions, Details, Functions, Input, Output, Pointers, Programming, Prototypes, Statements, Strings, Structures
![]() | Subscribe by Email |
|
Wednesday, September 14, 2011
Some details about pointers and strings in C...
There doesn’t exist anything in C like real strings. A string is an array. It can be defined as an array of characters. A string is terminated by a nul character “\0”. A string can be initialized normally as follows:
Char string[10];
String[0] = ‘c’;
String[1] = ‘a’;
String[2] = ‘t’;
String[3] = ‘s’;
Another way of initializing and declaring a string is as follows:
Char string[10] = “cats”;
The following statement asks compiler to allocate 10 bytes of memory for the string entered by the user. The string can store only 9 characters and the tenth one being used for “nul” character.
We can easily access strings and their constituent characters using character pointers. The string arrays are mostly used for storing char data type elements in the memory. The array of string pointers is preferred over two dimensional arrays of characters mainly for two reasons. The first reason is by implementing array of pointers, you can make more efficient use of available memory since it consumes lesser number of bytes for storing arrays of strings. The second main reason is that an array of string pointers makes the manipulation of strings easier. You can easily exchange the positions of strings in the array using pointers without touching their memory locations. This makes managing strings very convenient. Another way of initializing a string is by using pointers like
for example:
Char string[ ] = “cats”;
Char *a;
Here is a pointer pointing to the string “string”. Pointers are another suitable way to access string arrays and modify them as we want. Since strings are composed of characters, we should use pointers to characters. The types of the variable and the pointers must match otherwise an error will be generated. Be careful that you don’t use “&” (address of) operator to assign the address of a variable to the desired pointer. If you used it, the name of the array will be used as the address of that particular array. For this reason only we do not use “&” (address of) while passing a string to the function scanf(). For example see the following code:
Int a;
Char name[10];
Scanf(“%d%s”, &a, name);
The pointer allows us to access the required character in the string because we made it to point to that string. Functions can be passed to print labels and to call the function. Below is an example:
Void printlbl(char lbl[ ])
{
Printf (“the label: %s\n”, lbl);
}
int main()
{
char lbl2[] = "abc";
Printlbl(lbl2);
}
Now if we want to pass a pointer to the string array lbl2, we will use the following code:
Char *lblptr = lbl2;
The above declared pointer can be used in the function also like shown below:
Printlbl (lblptr) ;
After execution you will observe that both the statements give the same output. Now you will ask me why so? The answer for this is that when we pass an array as a parameter or an argument to a function, a pointer is automatically created without we knowing it and the arrays are passed by reference method automatically the way a pointer is passed. So the above written code can also be written the other way as follows:
void Printlbl(char *lbl)
{
printf("the Label: %s\n",lbl);
}
a code for dynamically allocated string is given below:
string = (char *)malloc(sizeof(char) * (len+1));
Some stings can be allocated dynamically. This is done when we don’t know how much long a string will go until the program is executed or when we don’t what size of string the user will enter.
Posted by
Sunflower
at
9/14/2011 09:01:00 PM
0
comments
Labels: Arrays, C Language, Characters, Code, Compiler, Data types, Declaration, Errors, Functions, Initialization, Memory, Pointers, program, software engineering, Strings, Variables
![]() | Subscribe by Email |
|
Tuesday, September 13, 2011
Some details about the types of pointers and arrays in C
Arrays are linear data structures whose data elements form a sequence. The elements of these linear structures are stored in memory as the contiguous memory locations and are represented as the same. These are the simplest data structures and are easy to be performed operations like insertion, deletion, searching, and traversal, sorting and merging. An array can store a finite number of data elements homogenous in nature i.e., they should be of same data type. The number of elements of the array represents the length or size of the array. The front and end index of the array are called lower bound and upper bound respectively. In C, the lower bound is always 0 and the upper bounds calculated as (size – 1). There are basically two types of arrays namely one dimensional arrays, two dimensional arrays and multi-dimensional arrays. The one –dimensional arrays are the simplest ones. Every name has a unique name. Each element of the array is represented by an index number or subscript of the element. An array is represented as:
Array name[ lower bound L, upper bound]
One - dimensional elements are implemented in C by allocating a sequence of contiguous memory location. The starting address of the first element of the array is called the “base address” of the array. C allows many searching algorithms namely linear search and binary search.
Two dimensional arrays are the arrays in which each element is itself an array. For example, a two dimensional array “A” can be represented as a table of M*N elements, where M is the number of rows and N is the number of columns. These arrays also like one dimensional array are stored as contiguous memory allocations. They must be linearized before storing.
These two dimensional arrays can be implemented in two ways i.e., row- major and column major. Row major implementation technique stores the arrays as rows whereas column major implementation technique stores arrays as columns i.e., it stores first row of the array as the first column in the memory. Algebraic Operations that can be performed on two- dimensional arrays are addition, subtraction, multiplication, division and transposition.
Pointer is a variable which stores the address of another variable. Pointers are the strongest as well as the weakest feature of the C programming language. Pointers are a mean to access and modify the address of a variable and its value. Pointers provide a way to implement dynamic allocation of memory and to improve the efficiency of program routines. But, they have a downside which is that they can cause your system to crash or hang if used in an incorrect way. There are no real arrays in a C program, but a chain of pointers. The name of the array is itself a pointer pointing to the first element of the array. There are many kinds of arrays. We will discuss them one by one. First is “array pointers”. These pointers store the address of the first element of the array. An array pointer can be declared as follows:
Int *a[10];
The second kind of pointers is “string arrays”. Strings as we know are nothing but just arrays of characters. A character pointer can be declared as:
Char name[ ]=”abc”;
Char *cpr;
The third type of pointer is called “const pointer” and can be defined as the constant pointer or a pointer to a constant. The fourth type of pointers is known as “structure pointers”. These pointers point to the structures. They too are declared by placing “*” in front of a structure name. Fifth type of pointer is called “object pointer” and points to an object the last and sixth type of pointer is the “THIS pointer” and it is used to automatically pass an implicit argument or a pointer to the object invoked in the function call.
Posted by
Ashish Agarwal
at
9/13/2011 11:18:00 PM
0
comments
Labels: Arrays, C Language, Engineering, Pointers, Software development, software engineering, String Arrays
![]() | Subscribe by Email |
|
Wednesday, September 7, 2011
How are strings and arrays defined in C?
A string can be defined as a group of characters, usually letters of the alphabet. When C is either to compare a string with another string, output it, copy it to another string, or whatever, the functions are intended to do what they are called to do until a null, which is a zero, is detected. Such strings are often called an ASCII-Z string. C treats a string as an array termination with a NULL character. C does not have a separate data type for strings and so strings exist as the arrays of characters. An extra byte must be left to store the NULL “\0”. Strings are declared as the arrays of characters. For example:
char name[25];
Strings can be fully or partially initialized depending on the requirements of the user. For example:
char fill[10] = { ‘a’, ‘ ‘, ‘r’,’o’,’p’,’l’,’a’,’n’,’e’,’\0’ };
There exist certain predefined functions for modifying strings. The library cstring.h houses all these functions. Strcpy() is used to copy one string into another string. Strlen() is used to determine the length of the string. Strcmp() is function for comparing two strings i.e., which one is bigger in terms of length. Strcat() is used to concatenate two strings. The second string is appended to the end of the first string. Strrev() is used to reverse a string alphabetically.
An array is a C derived data type and can be defined as a collection of variables of the same data type that are referenced by a common name. All arrays consist of contiguous memory locations where the lowest address corresponds to the first element and highest address corresponds to the last element. Arrays are useful when quite many elements of the same data type are needed to be stored and processed. The numbers in [ ] are called indices or subscripts. A string itself is an array of characters. Arrays are of two types namely one- dimensional arrays and multi dimensional arrays. Multi dimensional array comprises of elements which themselves are arrays. The general form of an array declaration is as follows:
Type array-name [size];
Here type states the data type of the array elements. The array name declares the name of the array, and finally the size defines how many elements will be there in the array. The data type of the array is called the base type.
A multidimensional array consists of M*N elements where M is the number of rows and N is the number of columns. It can be declared as follows:
Type array-name [rows][columns];
Where single dimensional arras can be read using a single loop, it takes nested loops to read and access the elements of a multi dimensional array. One loop is used to process the rows and the other one is used to process the columns. The arrays can be initialized during the run time as follows:
Type array-name [size 1]……….[size N]= {value list};
The value list consists of the array elements. C allows you to skip the size of arrays in an initialization statement. These types of arrays are called unsized arrays. For example:
Int abc[ ] = {1,2,3,5,7};
The best way to assign values to an array is using a "for" or "while" loop because it’s not affordable to write a separate initialization for every element. Arrays can be passed s parameters to the functions to maintain a structured design. Always keep in mind that in C subscripts start from 0. A string from an array of strings can be accessed by using pointers. But keep in mind that pointers hold only address of the strings. From there it is able to obtain the whole chunk of the contiguous memory.
Posted by
Sunflower
at
9/07/2011 06:35:00 PM
0
comments
Labels: Arrays, C, C Language, Characters, Copy, Data, Elements, Functions, Initialization, Length, Letters, Nested loops, Null, Output, Requirements, Size, Strings, User, Values, Variables
![]() | Subscribe by Email |
|
Tuesday, September 6, 2011
What are functions,variables and prototyping in C?
Variables or identifiers are the fundamental basic building blocks of a program. They are general terminology for objects, classes, functions, arrays etc. It can be defined as an arbitrarily long sequence of letters and digits and the first character must always be a letter. Upper and lower case letters are treated differently and all the characters are significant. C is implementation dependent. C is case sensitive as it treats lower and upper case letters differently. These are basically named storage, whose values can be manipulated during the execution of the program. There are 2 values associated with every variable namely r value and l value. The value of the variable is called r value whereas l value is the address in memory where the variable is located. Generally a variable is declared as :
type name;
A simple definition consists of a type specifier followed by a variable name. There are 2 types of variables: local variables and global variables. Local variables are declared inside a function and are available to that function only whereas global variable are declared outside main() and are available to all the functions.
A large program is difficult to manage and understand. Thus, it is broken down into different modules called functions which can be put together to form the complete program. So we can define a function as a sub program that acts on data and often returns a value. A good function contains a number of functions since it is easier to maintain update and debug. Each function has its own name. On the encounter of the function’s name in any part of the program the function is called. There are basically 2 types of functions namely built-in functions and user-defined functions. Built in functions are parts of the compiler package and are made available to the program by header files. For example: exit(), sqrt(), strlen(), size() etc. user defined functions are created by the programmer as per the requirements of the program. A function has 3 parts: function definition, function call and function prototype. In C a function must be defined before it is used anywhere. Generally a function is defined as:
type function-name(parameter list)
{
Body of the function
}
Here the type specifies the type of return value. The parameter list is the list of arguments passed to the function separated by commas. A function may be defined without any arguments or it can also have an open parameter. A function definition must have a return statement.
One of the most important features of C is function prototyping as it describes the function interface to the compiler. It can be defined as the declaration of the function that tells the program about the type of the value returned by the function and the number and type of arguments. Therefore, we can say that a prototype has following parts:
- Return type
- Name of the function
- Argument list
Function prototyping is essential as it allows a compiler to compare each use of the function with the prototype to determine whether the function has been invoked properly or not. This makes it easy for compiler to correct the errors. The function prototype and function definition must exactly agree on the function name, return type, and argument list. The prototype looks just like a function definition except that the code is missing. A prototype introduces a function to the program whereas a definition being a declaration also tells the program what the function is doing and how it is doing. C makes prototyping essential. We can skip the arguments in a prototype but not in the definition.
Posted by
Sunflower
at
9/06/2011 08:00:00 PM
0
comments
Labels: Arrays, C, C Language, Case sensitive, Classes, Compiler, Definition, Errors, Functions, Global, Identifiers, Local variables, Objects, program, Prototyping, Types, Values, Variables
![]() | Subscribe by Email |
|
Saturday, July 30, 2011
Black Box Testing Technique - Orthogonal Array Testing (OATS)
Orthogonal array testing enables you to design test cases that provide maximum test coverage with reasonable number of test cases. This type of testing can be applied to problems which has relatively small input domain but too large to accommodate exhaustive testing. Orthogonal array testing is more suitable in finding errors associated with faulty logic within a software component.
Orthogonal arrays are two dimensional arrays of numbers which possess the interesting quality that by choosing any two columns in the array you receive an even distribution of all the pair-wise combinations of values in the array.
The benefits of orthogonal array testing includes:
- lower execution time.
- lower implementation time.
- code coverage is high.
- overall productivity increases.
- the analysis of results take less time.
Orthogonal array testing uses the following terminology:
- Runs are the number of rows in an array.
- Factors are the number of columns in an array.
- Levels are the maximum number of values that can be taken on by any single factor.
Orthogonal array testing (OAT) helps in optimizing testing by creating an optimized test suite, detects all kind of faults, guarantees the testing of pair wise combinations, less prone to errors, simpler to generate and is independent of platforms and domains.
Posted by
Sunflower
at
7/30/2011 10:40:00 PM
0
comments
Labels: Arrays, Benefits, Black box testing, Columns, Design, Dimensions, Domain, Efficiency, Errors, OAT, Orthogonal Array testing, Pair, Productivity, Quality, Rows, Software testing, Test cases
![]() | Subscribe by Email |
|