Sie sind auf Seite 1von 4

Pointers: 1. Pointers are variables containing the memory addresses of other variables. a.

Declared using <datatype_of _variable_it_points> * <name> eg. Int * p Note that * can be placed anywhere between int and p, need not be just before p. b. Operators used are: i. & - used to get the address of the memory location. Eg. p=&a stores the memory address of the variable a in the pointer p. ii. * used to indicate the value stored at that address where the pointer points. Eg. Int *p; int a=10; P=&a; // points to the address of a *p=5; // changes the value of a to 5 2. Pointers which point to another pointer are also acceptable in C. a. Eg. Int a=5; Int *p, *pp;// declare 2 pointers P=&a;// p points to as address Pp=&p;// pp points to ps address **pp=5;// value of a is changed to 5, .it is *(*pp)=5 3. Note that the *can be confused with arithmetic multiplication and *pointer. 4. A pointer of a particular datatype can point ONLY to that type of data. A float cannot point to an int, etc. 5. Array name is a pointer. a. Int *p=<array name> //this means that the address of a[0] is assigned to p. b. Array name is a constant pointer variable and cannot be modified. Eg: int b; Int a[10]; a=&b; //invalid statement, gives error. 6. void *p; is used to declare a pointer without a particular datatype. Such a pointer can point to any datatype; a. Eg: void *a; *a=5; //this is invalid for the void pointer, also POINTER ARITHMETIC is invalid. 7. Void *p; p=null; //the pointer is now pointing to a null location. Can also be written as p=0; 8. Type Casting is possible in pointers. Eg: void *p; int a=50; (int *)p=&a; // this void pointer is converted to type int. 9. 10. 11. 12. If statement, loop, etc. cannot be used with condition involving pointers. An array of pointers can be declared by <datatype> *<name>[size] Eg: int *p[10]; Note that int(*p)[10]; is a pointer to an array. Pointers can be compared eg: if(p1==p2){}

13. POINTER ARITHMETIC: Certain arithmetic operations can be performed on pointers: Addition, Subtraction, Increment and Decrement. These are allowed only with constants.

a. When a constant number is added to a pointer, the actual addition taking place in the memory location is an addition of the number of bytes space of the datetype. Eg. An integer pointer when incremented by one actually adds a value of 4 bytes. Eg: int *p; int a[10]; p=&a; // this points to a[0] P=p+1; //pointer now points to a[1].. 4 bytes have been added to the memory address. More on array as a pointer: a) Consider int a[10][5] Here, a is a pointer to a pointer as it is a double dimensional array. In this suppose a[0] points to the first 1st element of the first row. a[3] points to the first element of the 4th row and so on. If you do an arithmetic operation like a[3]+2 the pointer is now pointing to the 3rd column of the 4th row, note that the row does not change. b) Note: Special case Consider two array pointes *a and *b pointing to two different positions in an array q[], we know that a+b is not a valid statement as per pointer arithmetic. However, since these points to two different parts of the same array, a-b is allowed here and will give the difference in the positions.

Pointers applied to functions: a) Syntax <datatype> *<fun_name>(parameters) Eg: int *fun(int a){} This is useful if you want to access a variable which is otherwise static with the help of this function which returns the address of the static variable. #Consider the situation where the variable inside the function whose address is returned using such a function is not static, then, as soon as the function is run and completed, such a variable is destroyed. This means that the memory location of that function will hold no value. This may give unexpected result or error. b) Another use of such a function returning pointer is that this function can be used as a parameter to another function. This is not possible in other cases.

Arrays: 1. Syntax of single dimensional array <datatype>a[size] a. Can be initialized by <datatype>a*size+=,a,b,b. If a,b is lesser than the size of the array others are initialized to 0 c. <datatype>a[size] will contain garbage value if not initialized.

2. Syntax of double dimensional array is <datatype>a[row size][col size]. a. Can be initialized by <datatype>a*size+=,,a,b,-,,x,u..-b. a[][b] ={1,2,3,4,5,6} gives an array of a[6/b][b] 3. Multidimensional arrays a. Can be initialized by <datatype>a[size][size2][size3]= ,,a,b,-,,x,u..-,,m,n-b. Number of elements = size1*size2*size3

Array size = bytes *no. of elements Eg. For int 2*no of elements. Functions And Scope, Life of Variables 1. Usage of variable before its initialization is permitted provided the variable is declared before operations are done on it. Eg.//Shows that the compiler recognizes a[] as it is outside the main() function. #include<stdio.h> char a[]; main() { printf(%d, a*1+); } a*+=,a, b, c-;

Output: b 2. Variables declared as static means that the life of the variable is till the end of the program even though it is a local variable and not a default variable.// Unlike java static variables can be modified. 3. Global variables have scope from the point where it is declared and have a life throughout the program. 4. Global variables are auto initialized to 0. 5. Global variables cannot be initialized to a variable. Eg. Int a=4; int b=a;//invalid. 6. Local variables are also known as auto-variables as they are made by the compiler during runtime. 7. By default the return type of function is taken as int. 8. Program starts in the main function. 9. When arrays are passed to a function the actual address of array is passed, any manipulation henceforth affects the array. // This can be used to modify arrays ..without returning them. 10. If the return type is void and a return statement is used in the function, it is valid provided there is only the word return Eg. void a() { return;}// this gives no error. DYNAMIC MEMORY ALLOCATION Dynamic memory allocation means that the memory is allocated at the runtime of the program and it is not pre-determined. 1. For functions used in this section header files stdlib.h have to be included. 2. void *malloc(parameter) is a function which is used to allocate a memory location. A) Parameter is the size in bytes. Usually functions such as sizeof(int) etc. are used here. 3. This function does not initialise the memory location , hence it will contain only garbage value. Usage: Int *x, I; X=(int *)malloc(10*sizeof(int));// we are type casting the void function before usage, Which is to store 10 integer variables. For(i=0;i<10;i++) x[i]=100+I; 4. void *calloc(numbers,size_in_bytes); a. Different from malloc() as here memory is allocated to 0 or whatever according to datatype. Note: Memory allocated by functions such as malloc() and calloc() will be available throughout the program.

General Important Notes: Caution while predicting output 1. Check for semi-colon(;) after if statements and loop statements. 2. If a statement tries to access a location which is after the string(char array) ends it will get garbage values 3. Check if functions like strlen() are within in print statements otherwise that will be printed and not the value returned by the function. 4. Indicate whether the program is actually ending or it is just in an infinite loop without anything being printed. 5. strlen() of hello abc is 9 and not 10 do not include null character. 6. To declare an array capable of storing 5 strings of max length 5 char a*5+*5+do not account for end of string(null) character.