Thursday, July 9, 2009

What is a pointer in C language?

It's a variable in memory that instead of holding a value, it holds an address location which serves as a "pointer" to another piece of data





example:





int c;


int * cp; //cp is a pointer


c = 3;





cp = %26amp;c; //cp now points to 3;


//%26amp; is the "address of" operator which returns the address of a variable


* cp = 4; //* is a C operator which means "dereference"


//what the * does it says, instead of using the pointer itself


//use the value pointed to... so when you do '* cp = 4;' you are actually changing the value of c








Pointers take a bit of practice and understanding. You can do basic arithmatic on them. Arrays are basically just a pointer to the first element of the array. c-strings are just a pointer to the first character in the string, really an array of characters.

What is a pointer in C language?
The other explanations are pretty accurate.





One thing to understand is that pointers are not just an abstract concept in C. A pointer literally represents the memory address (or virtual memory address) of some data being referenced.





Think of the memory of a computer as a bunch of mailboxes containing a single datum and each with it's own corresponding sequential address. The value in the pointer is the actual address of the mailbox.





This means that if you inadvertently assign the wrong value to a pointer, you'll be accessing a completely different range in memory than you thought you were with dire consequences. You can have two different pointers pointing to the same address and change the contents of the referenced value in ways that are not easy to track. You can do pointer arithmetic and go beyond the end of an array accidently reading or writing to data.





Just wanted to add some details.
Reply:[I'm weak in spelling. Ignore spelling errors]





A pointer is itself a variable which does not store normal constants but stores the address of memory location of other variables. Each variable is identified by it's address just like our houses are identified by the address by Post Dept. Now what is the use of pointer? Good Question. This can be understood more clearly in C++. It plays a very vital role. But this same pointer can be highly hazardous. How? Suppose you have a C program which stores the security information of your system or network, an alien code can get inside this program and with the use of pointers can change every single information. That's why pointers are not allowed in Java. Let's see a sample program:





#include ........


...


...





void main()


{


int c = 5;


int *iPtr;


// The variable iPtr is set to a INTEGER pointer.Means


// it can point to only integer variables.


iptr = %26amp;c;





// To store the address of a variable '%26amp;' symbol is used


// before the wanted variable. So in this case address of


// integer variable 'c' is stored in integer pointer iPtr


// Ususally an address of a location always starts with 0x





printf("Value of c (using pointer) : ", *iPtr);


/* It's a little confusing. Try to understand. To get the value stored in the address which in turn is stored in iPtr, we should use * in front of the pointer. Therefore *iPtr will yeild to 5. Confused??? Suppose the address of variable c is 0x4963. Therefore 5 will be stored in 0x4963. Now 0x4963 will be stored in iPtr. So *iPtr will check the value stored in 0x4963 w3hich is 5. Therefore *iPtr will yeild to 5. Understood? No? Read this comment again, you will understand slowly */





*iPtr = 44;


/* Now what happens? 44 will be stored in *iPtr which is 0x4963. Therefore 5 will get erased and 44 will get stored in 0x4963. Therefore now the value of c will be 44 */


printf("Value of c (direct using c) : ", c);


} //End of program








OUTPUT


------


Value of c (using pointer) : 5


Value of c (direct using c) : 44








Hope this helps.








Senthil
Reply:Pointers are a primary source of potential danger. Because they are typically unchecked, a pointer can be made to point to any arbitrary location (even within code), causing unpredictable effects. Although properly-used pointers point to safe places, they can be moved to unsafe places using pointer arithmetic; the memory they point to may be deallocated and reused (dangling pointers); they may be uninitialized (wild pointers); or they may be directly assigned a value using a cast, union, or through another corrupt pointer. In general, C is permissive in allowing manipulation of and conversion between pointer types. Other languages attempt to address these problems by using more restrictive reference types.


No comments:

Post a Comment