-
Pointer "*" and ?????"&"
Please Help Here.
I don't know the whole concept of
Eg.
int *Volume; // Pointer to Volume
or
void Calculate(int *amount);
and
Calculate(&counter);
Can Somebody explain to me the diferance between the * and &.Lots of programs
use them and lots of errors are caused by them but i don't know all the stuff
about them .
ThanX johan
-
Re: Pointer "*" and ?????"&"
"Johan Jacobs" <RooiJohan@webmail.co.za> wrote:
>
>Please Help Here.
>
>
>I don't know the whole concept of
>Eg.
> int *Volume; // Pointer to Volume
> or
> void Calculate(int *amount);
>
>and
>
> Calculate(&counter);
>
>Can Somebody explain to me the diferance between the * and &.Lots of programs
>use them and lots of errors are caused by them but i don't know all the
stuff
>about them .
>
>ThanX johan
>
Well.Informally int *Volume; means you are saying the compiler that Volume
is a pointer to integer. But at that time you are not allocating any memory
means you are not actually pointing to any valid thing.
consider the scene like this. You have your point finger but it may not always
be used to point to some direction. Just you use it whenever you fell necessary.
In the same way you when ever feel necessary allocate memory , that is, specifies
the valid direction by using malloc (or any other) or you may point to already
allocated memory like
Volume=&a; // where you already should define before like, int a;
When you use malloc(or new) you are poiting to newly allocated memory (at
run time) and when you are using & operator you are just pointing to already
allocated(by compiler) memory. That means incase of Volume=&a; both Volume
and &a would be pointing to same memory.
coming to
Calculate(int *amount);
Calculate(int& Counter);
Here this is not the case of allocating memory but the case of passing parameters.
In the first case you are passing the pointer to integer as parameter.
You invoke it like, Calculate(&amount); If you dont send pointer to amount
the changes made by Calculate will not be applied to amount.But by sending
the pointer to amount any changes made to the memory location that is pointed
by the pointer( nothing but amount) would be survived.
In the second case you are passing the parameter by Reference. That is you
invoke it with, Calculate(Counter);
In this case if you avoid the & sign in the defination of function then compiler
creates a new copy of formal parameter and initializes with the value of
actual param and then contiues , but the updates to counter would be lost,
since they are actually done on another memory location.
But as you are specifying it as pass by reference the compiler binds the
formal parameter with the actual parameter and any updates done would directly
effect the actual parameter that is Counter.
Please go through carefully and try Books On " THEORY OF PROGRAMING LANGUAGES"
for types of passing parameters.
All The Best.
Thanking you,
Yours,
P.GopalaKrishna.
-
Re: Pointer "*" and ?????"&"
Consider memory to be a large array. A pointer is the index into that array.
But the index is just a location, the data must be extracted by going into
the array. This is a pointer! The new and delete are needed because you
must tell the os that you have taken some memory so it won't give it to another
program.
The syntax is
int *x; //x is going to be the index into memory where one or more ints
will be stored.
x = new int; //get one int
x = new int[100]; get 100 ints.
you can dereference (get the data) x by
*x = 23; //first location
*(x+index) = 23; //other locations. cryptic!
x[11] = 23. //cleaner looking.
the & means take the address of something.
The address is the index in memory, its a pointer.
so
int z;
x = &z; //x points to z. usually used in function calls when the function
wants a pointer and your item is not a pointer.
Thats what your call did; the counter function needs a pointer but the variable
was not a pointer. So taking the address makes it work...
Pointers must be deleted
delete [] x; //give memory back to os.
Finally, in a function, & means the thing is passed as a pointer so it will
change if the function changes it. Leaving it off means that the value will
not be changed after the function finishes. Inside the function, the changes
are kept. Its still pointer concepts; the & means pointer so the function
is working with the same data as main because its the same address in memory.
Lack of & means the function works with a local copy of the data and leaves
the original memory alone. Arrays are always passes as pointers. Structs
and classes should always be passed as pointers because it speeds up the
code (the copy for the function does not have to be created because its using
the original).
y = 11;
foo(y);
int foo(&x)
{
x = 23; //this changes y to 23
}
vs:
int foo(x)
{
x = 23; //in here, x is 23. Y is still 11.
}
These are the basics. You can do mean looking things with pointers, not shown
because there is no point. Its left over from C. The old C programmers needed
to do strange things because the language was not as cleab C++ does not
need these things, but they will work and some programmers use them to intentionally
ruin readibility or because its what they know to do.
"Johan Jacobs" <RooiJohan@webmail.co.za> wrote:
>
>Please Help Here.
>
>
>I don't know the whole concept of
>Eg.
> int *Volume; // Pointer to Volume
> or
> void Calculate(int *amount);
>
>and
>
> Calculate(&counter);
>
>Can Somebody explain to me the diferance between the * and &.Lots of programs
>use them and lots of errors are caused by them but i don't know all the
stuff
>about them .
>
>ThanX johan
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks