Solved

Pointers and Pointers to Pointers

Posted on 2011-09-15
10
265 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
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:Slimfinger
Slimfinger earned 63 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 126 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 9

Assisted Solution

by:user_n
user_n earned 186 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 33

Assisted Solution

by:sarabande
sarabande earned 62 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 186 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 186 total points
ID: 36551306
0
 
LVL 13

Assisted Solution

by:Hugh McCurdy
Hugh McCurdy earned 126 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 70
Problem to save 10 165
Setting nameservers after res_init fails doing res_query 2 96
Installshield for Embarcadero EX 10.1 Berlin 4 40
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

770 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