Question:
what is the use of pointer in c++?
2008-07-28 05:42:01 UTC
what if we dont use pointers?
pointers are used to store the address of the given variable...but what is the need of storing address
basically i didn't get the concept of using pointers
pls help...!!!
Ten answers:
Trying to spread LOVE
2008-07-28 06:09:47 UTC
Hey thats a very simple one.....

You dont have to always use pointers...

But sometimes it is unavoidable...



Take array for eg. It internally uses pointers...

eg:- int a[10]

here "a" is a pointer and when u write a[5] it automatically adds 5 units to original address and returns the value stored over there(which is a[5])

Thus u see how pointer has made the maniupulation easy.



Its a weapon in ur hand. It may make things very simple as just shown in above eg. It is u who have to decide when and how to use it.....



For more query just a drop a mail and we may discuss it further...
manoj Ransing
2008-07-29 06:08:45 UTC
Pointers is one of the most important feature allowed in C. If you are beginner in C or C++ make sure that you understand the concept thoroughly. Without the proper knowledge of pointers there is simply nothing you can do in real life programs.

To understand the pointer you must understand the memory, and how the memory is used in programs. C variables are stored in memories. When you declare a variable like

int a = 20;

some memory is allocated for this in data segment of the process. This memory location can be seen at any time in the running program by referring to expression &a. So when you say

printf("%u",&a);

the program will show the address of this location. Now if you want a pointer to point at adress of a, you can declare it as

int *b = &a;

now *b, and a will have same value.



Pointers has many advantages and equal disadvantages.

Advantages:-

1) dynamic memory allocation

2) pass it to function and avoid creating copy of variable saving time and space

3) function pointer and void pointer are very handy when needed

4) in C++ virtual functions are useless without pointers

...

Disadvnatages:-

1) Memory Leak

2) Security - You can take pointer at any memory location and can try to read the data.



Thus pointer is an extremely important concept in C,C++ and get thorough with it.
Ying Ding Aing
2008-07-28 06:39:57 UTC
Okay, suppose you are writing a word-processing program. You need to use a string variable to hold the text of the document currently being written or edited, right? Your first instinct would be to hold the text ('buffer' in CS lingo) of the file in a regular, static C-string, and declare it to be very large. For example:

char textbuf[1024] ;

But some documents can be very long, thousands, or even millions of characters. For automatically generated text or xml files, they can even reach the billion-character mark. Would you want to say:

char textbuf[1048576] /* one megabyte */

or even

char textbuf[1073741824] /* one gigabyte */

That's going to make your program take up a lot of resources every time it is run, even if you're just editing a two-line file. It may crash your system, or not even run at all.



So the solution is to dynamically allocate memory from something called the freestore, or 'heap' of unused memory. That way, when your program is ready to edit the file, it simply has to find out how large the file is (not hard but I won't go into it here), and then allocate the that much memory + a little more so you can actually edit the file. A very smart program will figure out when it's about to reach the end of the buffer, and add more memory as needed. That way, your program only takes the resources it needs to meet its immediate needs.



You can do some more interesting stuff with pointers too, but dynamic data allocation is the basic purpose.
no1home2day
2008-07-28 05:49:43 UTC
The purpose of pointers is to allow a subroutine or function to change the value of the variable pointed to by the pointer.



Otherwise, you send a COPY of the variable, and even if the subroutine or function changes the variable, you're only changing a COPY, not the actual variable.



It's generally not encouraged. A function that returns a value based on input values is the encouraged method of changing a variable, but that's not always possible, so the pointer variable is still available, just in case you need to change three variables when one input variable is changed, as an example.
tunisino
2008-07-28 05:56:00 UTC
Pointer will let you dynamically manage your program memory usage, optimize ressources, modify variables by functions, extend to new data types
?
2016-10-15 09:09:40 UTC
the subject is that on the time whilst this methodology tries to delete the dynamic reminiscence that ptr factors to, ptr does not in actuality component to dynamic reminiscence. In slightly greater intensity: once you write the string literal "try", the compiler instruments aside a static area of reminiscence and places that string in there. once you tell ptr to point there, ptr purely factors to that reminiscence (which thus reasons the 5 bytes you purely allotted to be leaked, that's a bad element yet if you consider which you're in ordinary terms doing it as quickly because it won't ruin this methodology). Then once you tell this methodology to delete the dynamic reminiscence that ptr factors to, the laptop unearths that ptr factors to a static area of reminiscence, which won't be deleted. So it crashes, that's exactly what we would anticipate it to do. If what you honestly needed to do became into to repeat the contents of the static string "text cloth" into the hot reminiscence you allotted (and then loose that dynamic reminiscence), the physique of techniques to do it fairly is slightly diverse. look up applications which contain 'strcpy()' and 'memcpy()' for greater guidance.
2008-07-28 06:47:48 UTC
one word simple answer is that a pointer is a substitute variable name used inplace of actual variable.
2008-07-28 06:00:27 UTC
simply stre the address of another variable just like done in c
2008-07-28 06:25:47 UTC
basically it is a special variable which is use to strore the address of anothere variable...
some_guy
2008-07-28 09:06:32 UTC
Its used for dynamic memory allocation. Its useful when you need lot of memory and don't want to declare it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...