Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   A Beginner's Guide to Pointers (http://www.go4expert.com/articles/beginners-guide-pointers-t2464/)

Sanskruti 7Jan2007 14:25

A Beginner's Guide to Pointers
 

Introduction



Definition: Pointer is a variable which stores address of another variable.

To understand this definition properly, let us separate it into two statements:

1. Pointer is a variable.
2. It stores address of another variable.

Consider the first statement, it means that a pointer is a variable like any other variable such as i in the statement int i, f in the statement float f, c in the statement char c etc. Now consider the second statement, it states pointer stores address of another variable. And this is the difference between pointer and an ordinary variable. An ordinary variable stores value such as a whole number, real number, a character etc, where as a pointer stores address of another variable.

How pointers are declared and used?



See the following statement in C programming language:

int i = 327;

say 3843 is the address (or say location number) in the memory where the value 327 is stored. Now to retrieve this value from the memory, we can use pointer.
How? See this…..

int * ptr; this is pointer declaration. here * is read as ‘value at address’, it denotes that variable ptr is not an ordinary variable but it is a special variable i.e it is a pointer variable

ptr=&i; here & is ‘address of’ operator. It assigns address of i to ptr

Get the value of the variable ptr (address of i) by using the statement

printf(“%u”, ptr);

Answer:
3843

If you want to see value stored at the address use following
printf(“%d”, *ptr);

Answer:
327

What is the use of pointers?



All the beginners who are learning pointers wonder some time or the other, what is the use of pointers? The answer is, pointers are primarily used for constructing references, as well as in passing the data between different parts of a program.
Pointers are used in languages such as C, Free BASIC, C++, Pascal, Fortran, and most assembly languages.

Let us discuss pointers with reference to C programming language.



1.Pointers can be dereferenced to access the data stored at the address pointed to, or to invoke the pointed-to function.

2.Pointers can be manipulated using normal assignments and also pointer arithmetic.

3.The run-time representation of a pointer value is typically a raw memory address, but since a pointer's type includes the type of the thing pointed to, expressions including pointers can be type-checked at compile time.

Pointers are used for many different purposes in C.



1.Text strings are commonly manipulated using pointers into arrays of characters.

2.Dynamic memory allocation is performed using pointers.

3.Pointers to functions are useful for callbacks from event handlers.
A null pointer is a pointer value that points to no valid location (it is often represented by address zero).
Dereferencing a null pointer is therefore meaningless, typically resulting in a run-time error.
Null pointers are useful for indicating special cases such as no next pointer in the final node of a linked list, or as an error indication from functions returning pointers.
Void pointers (void *) also exist and point to objects of unknown type, and can therefore be used as "generic" data pointers. Since the size and type of the pointed-to object is not known, void pointers cannot be dereferenced, nor is pointer arithmetic on them possible, although they can easily be (and in fact implicitly are) converted to and from any other object pointer type.

Peter_APIIT 12Apr2007 12:51

Re: A Beginner's Guide to Pointers
 
Hello Sanskruti, you should start a new topic in c how to dynamic allocate memory because this features is quite useful and enhance flexibility of the program.

By the way, how to dynamic allocate memory for array of pointer and each pointer pointed to integer.

For example, int *array_ptr[10] contain 10 pointer and each pointer pointing to integer.


Thanks for oyur help.

Your help is greatly appreciated by me and others.

tailhook123 24May2007 21:25

Re: A Beginner's Guide to Pointers
 
Quote:

Originally Posted by Peter_APIIT
Hello Sanskruti, you should start a new topic in c how to dynamic allocate memory because this features is quite useful and enhance flexibility of the program.

By the way, how to dynamic allocate memory for array of pointer and each pointer pointed to integer.

For example, int *array_ptr[10] contain 10 pointer and each pointer pointing to integer.

The [n] form for array should automatically allocate the 10 pointers. Just like:

int a[10]

will automatically allocate 10 integers.

If you're referring to the integers themselves... you just allocate them before they're set.

int my_int;
int *ptr_int;

array_ptr[1] = &my_int; // & returns the address of the variable.

ptr_int = new int *; // if i remeber that properly
array_ptr[1] = ptr_int;

That said.. all this stuff is ancient code heh. New causes so many problems.. especially when you allocate something and then forget to free the space.

karri 5Jun2007 15:17

Re: A Beginner's Guide to Pointers
 
main()
{
int a[5] = {1,2,3,4,5};
int *ptr = (int*)(&a+1);

printf("%d %d" , *(a+1), *(ptr-1) );

}


in this code though &a is a pointer to pointer it is assigned to a pointer (ptr) by typecasting how is it possible;

shabbir 5Jun2007 15:26

Re: A Beginner's Guide to Pointers
 
karri, It looks like your post is not related to the above article and I would suggest you creating a new thread with appropriate title for your query.

sgs_guna 23Oct2007 11:26

Re: A Beginner's Guide to Pointers
 
This info abt pointers would impress Beginners more...

Also pls try 2 post more topics 4 beginners 2.....

Thanx...

vinay121 26Jun2012 21:41

Re: A Beginner's Guide to Pointers
 
i want video lecture to learn pointer and passing pointer to function... could u tell me site?

sumtaru 29Jun2012 19:10

Re: A Beginner's Guide to Pointers
 
good one for beginners. I want more examples.

samraj 6Jan2013 22:32

Re: A Beginner's Guide to Pointers
 
hi,
I am having a structure containing 80 fields in one file.
an extern structure in other file.

i am losing variable values between function calls.

how pointers can help me to solve this.???


All times are GMT +5.5. The time now is 23:08.