advantage of using pointer in C programming?

advantage of using pointer in C programming?
suoju1Asked:
Who is Participating?
 
lucky_jamesConnect With a Mentor Commented:
well, its the easiest way to pass on the objects.

Now you will think, we can always pass by value.
Right....we can pass.

But think of it when object is very large. It would lead to unnecessary usage of the memory.

Pointers become more important as you dont have and reference like thing in C. ( in c++ you have this facility).

This also enables you to store the whole collection using indexes. As you need not to look into the whole collection, just iterate thru the indexes and you can reach the required memory location. So, easy, quick way of navigation thru memory.

Hope it helps.
0
 
suoju1Author Commented:
thanks, i am talking about the C programming mostly.
0
 
lucky_jamesCommented:
Yeah, i have commented on C mostly. any other doubt do you have??
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
evilrixSenior Software Engineer (Avast)Commented:
Well, I don't think it should been seen in terms of advantage but rather choosing the right method of passing or the job. In C++ you have three choices: -

By value.

This is the simplest. A copy of your value is taken and lives only for the life of your function. Normally this is very simple; however, for types other than intrinsics this can be a very costly exercise. Also, since objects in C++ have have non-trivial copy constructors it can also result in unexpected side-effects (for example, the std::auto_ptr has move semantics and NOT copy semantics).

By pointers.

The same as C. You pass the address of an object or intrinsic type. This allows for changes inside a function to be modified outside a function. It's also far less costly to pass objects around by pointer as they are always intrinsic types and copying them has no side effects. The downside to pointers (amongst other things) are they can be confusing to use (pointer semantics are difficult for noobs and experts alike) and dereferencing them can be problematic as you need to know you are dereferencing a valid pointer (doing this to, for example NULL, will crash the program and/or have unexpected side effects).

By reference.

C++ only. Probably the simplex mechanism as the semantics are a combination of by value and by pointer. When you pass by reference you don't need to do anything specific to the calling semantics, you just seemingly pass by value. The declaration of the function will specify it expects a reference rather than a value. You end up with a synonym of the original object but without the copy so there are no side effects and it is efficient for passing non-intrinsic objects. The down side is that the caller has no idea they are passing by reference and not by value so the called function could modify what's been passed without the caller even knowing or expecting this to happen.

This is a very brief outline but hopefully it'll help bring some clarify for you.

-Rx.
0
 
peprConnect With a Mentor Commented:
It is questionable whethere existence of pointers should be called "the advantage". They simply exist. Technically, a pointer variable is the variable that stores the addres of the thing that the pointer points to.

If you take the real-world example of a house with a street address then "pointer variable" is a piece of paper where you write the address of the house. Anyone can find the same house when he/she copies the text from the paper. Now we can reformulate your question: "What is the advantage to use a piece of paper for writing down a street address?".
0
 
evilrixSenior Software Engineer (Avast)Commented:
Additional info you may find useful: -

http://www.embedded.com/story/OEG20010311S0024
0
 
suoju1Author Commented:
pepr: thanks for the example

Now we can reformulate your question: "What is the advantage to use a piece of paper for writing down a street address?".

but without the paper with the street address/pointer, we still can find the house right?

0
 
Infinity08Commented:
>> but without the paper with the street address/pointer, we still can find the house right?

only by accident/coincidence.


A pointer is the address of some object in memory. If you don't have that address, you can't find the object in memory.
0
 
peprCommented:
The pointer variable is a piece of memory. The paper piece is also a piece of memory. Say you would not have this extra piece of memory, where would you remember the address? Technically, the only other possibility is to have the address "hardwired" into some code (part of processor instruction). This is similar to dealing directly with the house. Say, you stand in front of it and you can see the only house. When I ask "What is the colour of the house?", you can answer immediately. The house is part of your surrounding space.

Now say, you were passed the address of the house on a piece of paper. You cannot answer immediately, but you can do it indirectly. You can find the house first and then discover the colour. Or you can use some ready-to-use procedure (say a the reality agent) to discover the colour -- just copy the address of the house to the letter and send it to his office.

In programming, you can make a copy of an object and work with the copy. (This is unrealistic with the house.) If the house is big, the copying would be extremely expensive for the purpose of determining its colour. The similar holds when programming. If some data structure is big in size, copying would be time and memory consuming (think about megabytes for better imagination, but tens, or hundreds or kilo bytes are more realistic).  In such case, it is more effective to share the knowledge about the same data structure -- to pass its address via a pointer value. The pointer value (the address) occupies 4 (or 8) bytes and copying the bytes is much more effective than copying kilobytes).
0
 
evilrixSenior Software Engineer (Avast)Commented:
The original question was, "advantage of using pointer in C programming?".

The simple answer is there is no advantage other than being able to do some things that you otherwise could not do! A pointer is a tool, if you need to use it you do so otherwise you don't. It's like saying (analogy alert!) what is the advantage of using a hammer in carpentry. Well, there is no advantage other than being able to do the job that the tool is designed for -- can't can't bang in nails with a saw!

-Rx.
0
 
evilrixSenior Software Engineer (Avast)Commented:
...there are, of course, times when using a pointer is advantageous. Such as passing by pointer rather than by value to avoid expensive copy and also to allow a function to modify data and for those modifications to be visible external to the function. There are times where there is no advantage, you wouldn't create a number of stack objects and then create pointers to them to refer to the stack based objects by pointer for no real good reason. Ultimately, choose the right tool for the job... if you need a pointer use one otherwise don't!
0
 
suoju1Author Commented:
can i put in this way:
pass by value: a local copy is needed, so it is memory expensive. it is like if you copy some one's file, you need a local hard disk space. oringinal value  is not changed.

pass by reference:a local copy is not needed, so it save memory. it can change the oringinal value.
0
 
suoju1Author Commented:
can i put in this way:
If you take the real-world example of a house with a street address

then "pointer variable" is a piece of paper where you write the

address of the house. Anyone can find the same house when he/she

copies the text from the paper. Now we can reformulate your question:

"What is the advantage to use a piece of paper for writing down a

street address?".


type of the varible: building
name of the varible: white_house
value of the varible: George_Bush
address of the varible: 123_washington_street

declare the varible: building white_house=George_Bush

declare pointer(paper): building *p = &white_house
value of p is: 123_washington_street

p reference to the white_house
*p is equal to the value of the varible white_house which is George_Bush

we can directly use the varible white_house whose value is George_Bush
or we can indirectly use the the pointer p to refer to the varible white_house

but how to explain the dereference which is *p??

thanks
0
 
peprCommented:
Besides the question whether George_Bush is of the real value of the white_house...

p should be called "pointer". The term "reference" is used for similar but slightly different kind of variable type.

For your example, the white_house (human-readable name of the variable) is a synonym for the address 123_washington_street. The content of the address can be assigned: white_house = George_Bush

Symbolically, you could assign:  p = 123_washington_street
the same way you can assign:  n = 5

When you write n in your program, you think about it as about 5. When you write p in your program, you should think about it as about (123_washington_street), nothing more. If (123_washington_street) is the address, then *(123_washington_street) refers to content of the space located at the address. If (123_washington_street) is stored in another variable named p, then *p is the content of the same space. To summarize: p is the name of the variable that contains 123_washington_street, the *p is George_Bush, because that value is stored inside the space located at the address 123_washington_street.

Dereference is that one extra step of accessing the value through the indirect reference.
0
 
ikeworkCommented:
mainly you are right (but i dont think the person you metioned wants to be considered as a house;)
i think we had too much analogies here, though i think they were very good :)

>> but how to explain the dereference which is *p??

that can be explainted easier with real c i guess. image this:

int value = 5;
int *ptr_to_value = &value;

ptr_to_value now holds the memory-address where the 5 is stored. now either you can change the value directly like this:

value = 6

or you use the pointer and dereference is to change the value:

*ptr_to_value = 6

you can use it as well to read the data, which is referenced by the pointer:

int value_again = *ptr_to_value;

now value_again would be 6


hope it helps :)

ike
0
 
ikeworkCommented:
>> Besides the question whether George_Bush is of the real value of the white_house...

lol .. or maybe he is a pointer as well .. if he was a pointer, who is he dereferencing .. ;)

i fall in love with those analogies here .. :)
0
 
ikeworkCommented:
>> but how to explain the dereference which is *p??

access (read/change) the value at the address stored in p
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> it is like if you copy some one's file, you need a local hard disk space. oringinal value  is not changed.

Windows analogy: -

Pass by value is like copying a file from one drive to another, so you end up with two copies
Pass by pointer (careful, pass by reference means something else in C++) is like creating a Windows shortcut on one drive to the file on the other.
0
 
suoju1Author Commented:
int value = 5;
int *ptr_to_value = &value;--->is a reference

*ptr_to_value = 6-->is a dereference?? why we call it a dereference??this way ptr_to_value still refer to address of value.

actually i understand all of your talking, i just curious what this term dereference really meaning.

thanks

0
 
ikeworkCommented:
its just the terminology, this word describes that action. wiki says this at http://en.wikipedia.org/wiki/Reference_(computer_science)

"Accessing the value referred to by a reference is called dereferencing"

the term "dereference" means something like to retrace, to backtrack, to trace back, like follow the way back to the origin/source. but i better leave that to native speakers. i guess they can explain the word better than me. thats just what my dictionary says .. ;)

ike
0
 
ikeworkCommented:
its always a problem finding new words in science to describe things. just think about the atom. its not so atomar as people thought that time right?  ;)
0
 
Infinity08Commented:
>> actually i understand all of your talking, i just curious what this term dereference really meaning.

See your other question for that :

http://www.experts-exchange.com/Programming/Languages/C/Q_22990690.html
0
 
ikeworkCommented:
>  i don't think  *yPtr  "get rid of" the reference, or "dereference", as yPtr still refer the address of y.

thanks for the link infinity. i think now i understand what confuses him/her.  the combination of the prefix "de-" and the "get rid of"-phrase. both dont mean, that the pointer gets unusable, corrupted or changed.
0
 
suoju1Author Commented:
the term "dereference" means something like to retrace, to backtrack, to trace back, like follow the way back to the origin/source. but i better leave that to native speakers. i guess they can explain the word better than me. thats just what my dictionary says .. ;)

i check all the online dictionary, and can not get even the explain about the word.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.