Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pointers and Pointers to Pointers

Posted on 2011-09-15
10
Medium Priority
?
275 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 252 total points
ID: 36545796
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:Bill Nolan
Bill Nolan earned 252 total points
ID: 36545955
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 504 total points
ID: 36546140
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Assisted Solution

by:user_n
user_n earned 744 total points
ID: 36547577
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 35

Assisted Solution

by:sarabande
sarabande earned 248 total points
ID: 36548063
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
 

Author Comment

by:thenthorn1010
ID: 36551236
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 744 total points
ID: 36551300
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 744 total points
ID: 36551306
0
 
LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 504 total points
ID: 36551729
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
ID: 36560655
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

885 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