Solved

Pointers and Pointers to Pointers

Posted on 2011-09-15
10
264 Views
Last Modified: 2012-05-12
I am reviewing some C and C++ source code, and I was wondering if someone could answer a question for me. What are the differences and uses for pointers and pointers to pointers in C and C++? How are they used in the C/C++ language versus other languages like VB, C#, and Java?
0
Comment
Question by:thenthorn1010
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 63 total points
Comment Utility
You will find a comprehensive description at http://www.cplusplus.com/doc/tutorial/pointers/ - way better than I could explain it to you here.

>> How are they used in the C/C++ language versus other languages like VB,
>> C#, and Java?

Well, in short: There is nothing that can be compared to pointers in these languages, since they are basically all compiled into 'P-code' (http://en.wikipedia.org/wiki/P-code_machine)

0
 
LVL 7

Assisted Solution

by:Slimfinger
Slimfinger earned 63 total points
Comment Utility
jkr is correct, although the 'ref' keyword in C# provides a bit of that functionality.
0
 
LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 126 total points
Comment Utility
It occurs to me that, since you are reviewing code, that you might have specific questions.  If you post a bit of code where you are confused, perhaps you can get a specific answer.

It might also help to know your background.  Do you already  know one or more of Java, C# and VB?
0
 
LVL 9

Assisted Solution

by:user_n
user_n earned 186 total points
Comment Utility
for me pointers in java are not the same thing that are pointers in C.
http://en.wikipedia.org/wiki/Pointer_%28computing%29
0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 62 total points
Comment Utility
pointers in C and C++ are same. they are pointing to an address in virtual memory where the variable they were pointing to resides.

a pointer to pointer is a pointer to a pointer variable or a pointer to the first element of an array of variables.

Sara
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:thenthorn1010
Comment Utility
I am a software developer whom has been using VB .NET, C#, Java, C++, and several other languages across a spectrum of projects that do not last a long time. Due to recent downturns in the market, I have been picking up a lot of skills in various programming languages, but I am not on a project long enough to become "advanced" in it. So, I find myself constantly looking for answers on all forums instead of being able to know more answers on my own. I have been on and off working for five years, and my background began with C/C++ and quickly moved into VB. NET. With all of the programming language changes and not very much familiarity with just one, I am versatile but it takes me a while to get some of the projects done because I have to research some syntax that I am not used to, due to the various languages. I hope this gives a background on myself a little bit more.
0
 
LVL 9

Assisted Solution

by:user_n
user_n earned 186 total points
Comment Utility
http://en.wikipedia.org/wiki/C_Sharp_%28programming_language%29
In C#, memory address pointers can only be used within blocks specifically marked as unsafe, and programs with unsafe code need appropriate permissions to run. Most object access is done through safe object references, which always either point to a "live" object or have the well-defined null value; it is impossible to obtain a reference to a "dead" object (one that has been garbage collected), or to a random block of memory. An unsafe pointer can point to an instance of a value-type, array, string, or a block of memory allocated on a stack. Code that is not marked as unsafe can still store and manipulate pointers through the System.IntPtr type, but it cannot dereference them.
Managed memory cannot be explicitly freed; instead, it is automatically garbage collected. Garbage collection addresses the problem of memory leaks by freeing the programmer of responsibility for releasing memory that is no longer needed.
In addition to the try...catch construct to handle exceptions, C# has a try...finally construct to guarantee execution of the code in the finally block.

http://en.wikipedia.org/wiki/Reference_%28computer_science%29

http://cslibrary.stanford.edu/106/
0
 
LVL 9

Assisted Solution

by:user_n
user_n earned 186 total points
Comment Utility
0
 
LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 126 total points
Comment Utility
Thanks for the background.  Perhaps it would help you to do assignments while looking for work.  I do that even when not looking for work (unless I'm really busy).

One idea for pointers in C++, would be to create a class that contains, say, some basic information about some (fictitious) people.  Say, Name and height (in inches or cm).  Put that information into a flat text file.  Read it in an make objects and place them in an array as you go.  Print the results.

Then in phase 2, sort the array using some sort algorithm.  (You should be able to find one or you could ask for help here.)  You could start with bubble sort.  It's simple but SLOW.  Speed doesn't matter for  under a dozen records.  

Then, in phase 3, instead of sorting the array, make an array of pointers to that array and sort the array of pointers.

This will give you practice with pointers and may help you to understand.

Or you could try something else.  The point that I'm trying to make is write some code and learn from it.  If you get stuck, post some code and ask for conceptual help.

It would be better if you say you are trying to learn so the experts can try to teach, instead of just writing a quicksort algorithm for you.

Just my 2 cents.
0
 

Author Closing Comment

by:thenthorn1010
Comment Utility
Thank you to everyone for the great ideas on how to review pointers and the definitions of pointers. I have a good starting point now to look through for reviewing pointers.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 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

8 Experts available now in Live!

Get 1:1 Help Now