Well the most difficult topic to understand for any learner of C or C++ is Pointers.
Starting of with the Definition of Pointers :
Pointers are variables that hold a memory address, usually the location of another variable in memory.
The pointer is one of the strongest and useful feature of C and C++. And therefore the correct understanding and the correct use of pointers is the most critical factor in being a successful programmer.
Talking about my personal experience. Its been three years now working on C and C++ and I have worked on more than 12 projects including projects involving file handling also projects working with artificial intelligence and out of those 12 only 4 projects involved the use of pointers. When I say so i don't mean that using pointers is useless or I don't have sufficient knowledge of pointers. But always prefer the alternatives unless you really need pointers.
You can use pointers when :
So the above code is intended to assign the value of a to b using pointers.
So thats all about the basics of pointers in C and C++. We will talk about an advanced level of pointers in our next few articles. If you have any Query regarding this article do comment or contact us and we will be there for you.
Well here at Code Catalogue we will give you the best explanation to this topic.
Starting of with the Definition of Pointers :
Pointers are variables that hold a memory address, usually the location of another variable in memory.
The pointer is one of the strongest and useful feature of C and C++. And therefore the correct understanding and the correct use of pointers is the most critical factor in being a successful programmer.
Talking about my personal experience. Its been three years now working on C and C++ and I have worked on more than 12 projects including projects involving file handling also projects working with artificial intelligence and out of those 12 only 4 projects involved the use of pointers. When I say so i don't mean that using pointers is useless or I don't have sufficient knowledge of pointers. But always prefer the alternatives unless you really need pointers.
You can use pointers when :
1.
You need reference semantics.
Sometimes you want to pass an object using a pointer (regardless of how it was
allocated) because you want the function to which you're passing it to have
access that that specific object (not a copy of it). However, in most
situations, you should prefer reference types to pointers, because this is
specifically what they're designed for. Note this is not necessarily about
extending the lifetime of the object beyond the current scope, as in situation
1 above. As before, if you're okay with passing a copy of the object, you don't
need reference semantics.
2.
You need polymorphism. You can
only call functions polymorphically (that is, according to the dynamic type of
an object) through a pointer or reference to the object. If that's the
behaviour you need, then you need to use pointers or references. Again,
references should be preferred.
3.
You want to represent that an
object is optional by
allowing a
nullptr
to be passed when the object is being
omitted. If it's an argument, you should prefer to use default arguments or
function overloads. Otherwise, you should prefer use a type that encapsulates
this behaviour, such as std::optional
(introduced in C++17 - with earlier C++
standards, use boost::optional
).
4.
You want to decouple
compilation units to improve compilation time. The
useful property of a pointer is that you only require a forward declaration of
the pointed-to type (to actually use the object, you'll need a definition).
This allows you to decouple parts of your compilation process, which may
significantly improve compilation time. See the Pimpl idiom.
5.
You need to interface with a C
library or a
C-style library. At this point, you're forced to use raw pointers. The best
thing you can do is make sure you only let your raw pointers loose at the last
possible moment. You can get a raw pointer from a smart pointer, for example,
by using its
get
member function. If a library
performs some allocation for you which it expects you to deallocate via a
handle, you can often wrap the handle up in a smart pointer with a custom
deleter that will deallocate the object appropriately.
Source : http://stackoverflow.com/questions/22146094/why-should-i-use-a-pointer-rather-than-the-object-itself
Advantages of Pointers :
- Pointers Provide the means through which the memory location of a variable can be directly accessed and hence can be manipulated in the ways as required.
- Pointers support C++'s and C's dynamic allocation routines.
- Pointers can improve the speed and efficiency of your programs if uded correctly.
Disadvantages of Pointers :
- Pointers are the strongest feature but at the same time they can be dangerous and vulnerable features of C and C++. Uninitialized Pointers or Wild Pointers can even cause a system crash.
- Debugging programs containing pointers can really be a tedious job.
- Dangling Pointers can cause logical errors which a compiler sometimes cannot detect.
So winding up with the theory lets come to the use of pointers. How to use pointers in C and C++?
C :
Consider the declaration,
int i = 3 ;
This declaration tells the C compiler to:
(a) Reserve space in memory to hold the integer value.
(b) Associate the name i with this memory location.
(c) Store the value 3 at this location.
Consider the declaration,
int i = 3 ;
This declaration tells the C compiler to:
(a) Reserve space in memory to hold the integer value.
(b) Associate the name i with this memory location.
(c) Store the value 3 at this location.
main( )
{ int i = 3 ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
}
The output of the above program would be:
Address of i = 65524
Value of i = 3
So we can very well see that the storage specifier ie %u and %d helped us to print the address and value respectively. But the most important thing is that for printing the address an '&' was used.
So remember these 2 points : 1) Read '&i' as address of i. 2) Read '*i' as contents of i which we will be discussing later.
Now taking the example of '*i'.
main( )
{ int i = 3 ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
}
The output of the above program would be:
Address of i = 65524
Value of i = 3
Value of i = 3
So taking the last line of our syntax lets first convert it into English.
printf ( "\nValue of i = %d", *( &i ) ) ; = Contents of (address of i)
which means print the contents in that specific location.
Now let us take to the maximum level.
main( )
{ int i = 3 ; int *j ;
j = &i ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
}
The output of the above program would be:
Address of i = 65524
Address of i = 65524
Address of j = 65522
Value of j = 65524
Value of i = 3
Value of i = 3
Value of i = 3
Translation in English:
j = &i ; = pointer j takes the value of address of i.
I suppose this line makes the other lines very clear.
But the confusion arises in printing 'j' and '*j'
So value of j is the address of i but when i write '*j' i mean contents at that address.
We will discuss about functions like malloc,calloc etc in our next articles.
Coming to C++
We have almost everything same except the dynamic allocation in C and C++ which we will be
discussing in our next articles.
But Lets look up to the syntax.
main()
{ float a,b;
int *ptr;
cin>>a;
ptr = &a; // ptr takes the address of a
b = *ptr; // b takes the value of contents at the address stored in ptr
cout<<a<<'\t'<<b<<'\t'<<c<<'\t';
}
Now this gives an output of :
So thats all about the basics of pointers in C and C++. We will talk about an advanced level of pointers in our next few articles. If you have any Query regarding this article do comment or contact us and we will be there for you.
No comments:
Write comments