C Programming Reference

January 7, 2017 | Author: R_Aranganathan | Category: N/A
Share Embed Donate


Short Description

Download C Programming Reference...

Description

C Programming Reference >> C Basic Programming Tutorials >> Pointersquisite Knowledege 21/07/07 - 12746 Views - Ratings :

4.29 of 5 / 87 Votes

Level - Beginner

Pointer is the feature which has grabbed most attention in C Programming combined all other features . Pointers are most powerfull feature but can be also most dangerous if abused. The pointers in C are used in my task which cannot be achieved by any other mean, such as dynamic memmory allocation, changing value using function, dynamic data structures etc. . They are also much much efficient in use of system resources than thier counter part and can be used to enhance your program speed and reduce its size. The success of a c programmer depends crucially on his understanding of C Pointers and that us reason why we are going to discuss them here in painstaking detail. Contents 1. Pointer Fundamenta 2. Pointer Variable 3. Pointer Assignment 4. Pointer Conversion 5. Pointer Arithemetic 6. Pointer Comparison 7. Arrays And Pointers 8. Multiple Indirection 9. Pointer Intialization 10. Pointer To A Function 11. Dynamic Memmory Allocation Using Pointers 12. Restrict Pointers 13. Pointer Drawbacks

1. Pointers Fundamentals In the simplest term pointer is a nearly integer variable which stores a memmory address of a computer which may contain other variable or even another pointer. If a variable contains address of another variable than it is said that first variable points to second. Pointer can also be represented as a refrence to another variable but there is very subtle diffrence in the two statements which is mostly dependent upon situation and enviorment. Pointer is generally of size of integer on the machine but it may be of diffrent type which indicates the type of variable the pointer is pointing to decides pointers properties and behaviour. Pointer of one type cannot be implicitly converted from one type to another but can be explicitly converted using type casting. In such a conversion a pointer always assumes that it is point to a object of its type but reality may differ and if used incorrectly may lead to disasters including permanent machine damage.

Also it is important to note that all operation perform on pointers are done through two operators '*' (Star) and '&' (Ampercent). '&' is a unary operator that returns a memmory address of a variable. '*' is complement of '&' and return value stored at a memmory location stored in a pointer. '*' can interpreted as statement "at address" while '&' can be interpreted as statement "address of". Back To Top 2. Pointer Variable Declaring a pointer variable is quite similar to declaring an normal variable all you have to do is to insert a star '*' operator before it. General form of pointer declaration is type* name;

where type represent the type to which pointer thinks it is pointing to. Pointers to machine defined as well as user-defined types can be made. Multiple pointers of similar type can be declared in one statement but make sure you use * before every one otherwise they will become a variable of that type. Back To Top

3. Pointer Assignment This is a type of expression used to assign value of one pointer to another using assignment operator '=' . In this value of right hand side points to memmory address of variable stored in left hand side pointer. As a result both pointers point to same memmory location after this expression. Pointer of similar type can be used in expression easily as shown below but for diffrent type pointers you need to type cast them as shown in next section. #include int main () { char ch = a; char* p1, *p2; p1 = &ch; p2 = p1; // Pointer Assignement Taking Place printf (" *p1 = %c And *p2 = %c", *p1,*p2); // Prints 'a' twice

return 0; }

Back To Top 4. Pointer Conversion Pointer Conversion is a very powerfull yet very dangerous feature. Before concept of pointer conversion you must understand the concept of a void pointer. Void pointer technically is a pointer which is pointing to the unknown. Void pointer has special property that it can be type casted into anyother pointer without any type casting though every other conversion needs an type casting. Also note that in C++ even void pointer needs an type casting so to maintain the compatibility you may want to type cast void pointer anyway. Also in dynamic memmory allocation function such as malloc ( ) and alloc ( ) returns void pointer which can be easily converted to other types. Also there is a pointer called null pointer which seems like void pointer but is entirely diffrent. Null pointer is a pointer which points to nothing. Infact it points to the base address of you CPU register and since register is not addressable usage of a null pointer will lead to crash or at minimum a segmentation fault. Also be careful while typecasting one pointer to another because even after type casting your pointer can point to anything but it will still think it is pointing to something of it declared type and have properties of the orignal type. Type conversion is a powerfull feature but yet it may lead difficult to remove bugs and crashes and should be used with uttermost vigilance. It may also lead to unexpected and unreliable results but program would compile succesfully. Code below shows a type casting of one pointer into another #include int main () { int i = 10; char* p1 int *p2; p2 = &i; p1 = (char *) p2; // Type Casting and Pointer Conversion printf (" *p1 = %c And *p2 = %d", *p1,*p2); // Output maybe unexpected depending critic return 0; }

Back To Top 5. Pointer Arithmetic Pointer arithemetic is quite diffrent from normal arithemetic unless and until you are work on char type pointers, reason being they are 1 byte (special thnks to Emmanuel Deloget for pointing mistake) long under all enviorments. Not all artihemetic operations are defined in pointers. You can increment them, decrement them, add and subtract integer values from them. You even can subtract two pointers.But you cannot add two pointers, mulitply, divide,modulus them. You can not also add or subtract values other than integer. Now pointer artihemetic may look a little wierd but it has a deep sense attach to it. Now consider a pointer X , its current value that address it is pointing to is 1000 (just assuming).We make another assumption about the size of the data types. Size of data type is machine dependent, for example int can be 16,32, or 64 bit long depending upon your machine. Now if this X pointer is char type(assumed 1 Byte or 8Bit long) than X++ will have value 1001 and X-- will have value 999. Now if this X pointer is integer type (assumed 2 byte or 32 bit long) than X++ will have value 1002 and X-- will have value 998. Again if this X pointer is float type (assumed 4 Byte or 32Bit long) than X++ will have value 1004 and X-- will have value 9996. Also if this X pointer is double type(assumed 8 Byte or 64 Bit long) than X++ will have value 1008 and X-- will have value 992. Do you see the pattern here. Reason is when you increment a pointer of certain base type it increase it value in such a way that it points to next element of its base type. If you decrement a pointer its value decrease in such a way that it points to previous value of its base type. So increment as well as decrement in fixed quanta of size of the base type. You can add or subtract any integer value, in such case value of pointer get increase and decrease by the productv of the value to be added or subtract and size of the base type. Pointer of user defined types such as structures and union also increase by the quanta of thier bit values which can be determined using sizeof operator. Pointer arithemtic in C may look a bit strange but it is extensively used in programming and provides unmatched efficiency in performance of task such as accessing an array. Back To Top

6. Pointer Comparison

Two pointers can be compared no matter where they point. Comparison can be done using , =, = operators. Though it is not forcibly implied but comparison of two pointers become sensible only when they are related such as when they are poiniting to element of same arrays.Comparison of two unrelated pointers is unpredictable and your code should not rely upon it. All comparison are generally done on basis of memmory organization in the host machine. Following C source code shows a potential use of pointer comparison in C #include int main () { int data[100]; int* p1; int *p2; for (int i = 0; i p2) { printf ("\n\n p1 is greater than p2"); } else { printf ("\n\n p2 is greater than p1"); } }

Back To Top 7. Array and Pointers Array and Pointers in c are very closely related. Infact they are so similar to each othere in nature that they can be used interchangebly in each other positions most of the time. Important link joining them is that array name without the brackets is the pointer name and other end a pointer can be indexed as if its an array. Consider following C Source Code for better understanding in which cycle through the elements of array using a pointer as a array name and through pointer arithemetic. #include int main () { int data[100];

int* p1; for (int i = 0; i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF