pointer

hy..

i confused..,in what condition we must use pointer

and..if you not busy,Would u give me an exercise
just simple program that must use pointer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include "stdafx.h"
#include<iostream>

using namespace std;

void swap(int *, int *);

int main()
{
	int a=2, b=6;

	cout<<"variable 'a' before swap: "<<a<<" variable 'b' before swap: "<<b<<endl;

	swap(&a, &b);

	cout<<"variable 'a' after swap: "<<a<<" variable 'b' after swap: "<<b<<endl;

	system("pause");
	return 0;
}

void swap(int * pa, int * pb)
{
	int temp;
	temp=*pa;
	*pa=*pb;
	*pb=temp;
}


It's kind of program that needs using pointer. If you change function swap in this shape:

1
2
3
4
5
6
7
void swap(int pa, int pb)
{
	int temp;
	temp=pa;
	pa=pb;
	pb=temp;
}

it wouldn't do its task. Actually, it'll swap this two numbers (I mean a and b), but when program will go out of this function, it'll destroy local variables, so there will be no effect. If you use pointers, you do something directly on adress in memory, so if you change something in function, it'll also be changed when you leave it.
i see
thnx mtwee
bye the way, in some situations, pointers can be more efficient
for example:

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main ()
{
           //i want to create a variable only for a short time
           int *count;   //creating my variable, notice the sintax '*'
           count=new int;   //this will create the variable;
           *count=2;    //this will set the value of count to 2
            delete count;   //this will delete count leaving the RAM free for something else
                                    //this is mostly useful in BIG projects 


hope this helped
very2 help..thnx

so..I can use pointer when I want to save our value to a memory address
Yes. You can do sth directly on value that is stored in memory adress. Pointers give you an access to this value, by giving an adress. They are also effective, because, for example, when you give variable type int as an function argument, program makes a copy of this, so it takes more memory. If you use pointer (type is int *), it doesn't. It works directly on value in memory.
i undrstnd now
thanx u v much

can u gve me an exercise
I think a lot of this is, well, not too correct.

Largely, pointers are needed only when memory has to be allocated dynamically, however there are various other
edge cases as well. But for the C++ student, the edge cases can be ignored. So the real question is: "when do I
need dynamic memory allocation"? You need it in a couple of cases:

1. You need to allocate a number of elements [in an array] that is known only at runtime, not at compile time.

2. You need the lifetime of a variable to exceed the scope in which it is declared. Let's say I have a Thread object
that encapsulates a thread of execution. I have a function that creates a Thread object and wants to return it:

1
2
3
4
Thread* MakeThread() {
    Thread* thd = new Thread();
    return thd;
}


I want the lifetime of the Thread instance to exceed that in which it is declared (because in this case I want to return
it to the caller): it is instantiated within the function MakeThread(), meaning that it would be destroyed at the end of
MakeThread() if I just allocated it on the stack (instead of dynamically allocating it).
largely, thats correct:
for what you are trying to do, i would rather you dont use pointers but references:

you can implement the function viz

1
2
3
4
5
void swap(int &first, int &sec){
        int tmp = first;
        first = sec;
        sec = tmp;
}

since you have passed by reference, the first and sec values will be swapped appropriately.
Pointers are use on Polymorphism
thnx v much
incidentally, you can use pointers to 'clone' variables
like so:
1
2
3
4
5
#include //blah blah blah
int count;//creates a variable
int *num;//creates pointer
num=&count;//makes the adress in memory of the pointer equal to the adress of count
                     //if either one of those values changes, the other will to 
Topic archived. No new replies allowed.