Solved

advantage of using pointer in C programming?

Posted on 2007-11-29
25
2,906 Views
Last Modified: 2008-02-01
advantage of using pointer in C programming?
0
Comment
Question by:suoju1
  • 6
  • 6
  • 5
  • +3
25 Comments
 
LVL 7

Accepted Solution

by:
lucky_james earned 25 total points
Comment Utility
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
 

Author Comment

by:suoju1
Comment Utility
thanks, i am talking about the C programming mostly.
0
 
LVL 7

Expert Comment

by:lucky_james
Comment Utility
Yeah, i have commented on C mostly. any other doubt do you have??
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
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
 
LVL 28

Assisted Solution

by:pepr
pepr earned 25 total points
Comment Utility
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
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
Additional info you may find useful: -

http://www.embedded.com/story/OEG20010311S0024
0
 

Author Comment

by:suoju1
Comment Utility
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
 
LVL 28

Expert Comment

by:pepr
Comment Utility
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
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
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
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
...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
 

Author Comment

by:suoju1
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:suoju1
Comment Utility
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
 
LVL 28

Expert Comment

by:pepr
Comment Utility
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
 
LVL 20

Expert Comment

by:ikework
Comment Utility
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
 
LVL 20

Expert Comment

by:ikework
Comment Utility
>> 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
 
LVL 20

Expert Comment

by:ikework
Comment Utility
>> but how to explain the dereference which is *p??

access (read/change) the value at the address stored in p
0
 
LVL 40

Expert Comment

by:evilrix
Comment Utility
>> 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
 

Author Comment

by:suoju1
Comment Utility
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
 
LVL 20

Expert Comment

by:ikework
Comment Utility
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
 
LVL 20

Expert Comment

by:ikework
Comment Utility
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
 
LVL 20

Expert Comment

by:ikework
Comment Utility
>  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
 

Author Comment

by:suoju1
Comment Utility
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
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now