Solved

pointer to an array

Posted on 2004-10-16
5
280 Views
Last Modified: 2010-04-17

Hello Experts,

I need to access the nth element of an array that I have only got a pointer pointing at.

// That is,  I have declared a pointer

            int[] *stackptr;

// then I point it at an array of integers

             stackptr = new int[50];

// now I need to access the nth element of the array

              (*stackptr)[n];

//  What is my syntax problem here?

Thank you,

-TH










0
Comment
Question by:travishaberman
5 Comments
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 12330935
This is the classic confustion in C between arrays and pointers.  In C, an array is the same as a pointer.
When you create the array of int:
 stackptr = new int[50];
you are also creating a pointer to an int that is 50 int's long.
 int *stackptr=new int[50];

The declaration
 int[] *stackptr;
is actually an array of int pointers.  Which is the same as
 int[][] stackptr;

To dereference a pointer use the * operator.  To dereference an array use a [] operator.

So to reference the Nth element in an array
  stackptr[n]   = the Nth element in stackptr array
OR
  *(stackptr+5)  Add 5 int's to the address pointed by stackptr and get its value.
Whenever you add a pointer, the C compiler always knows what you are pointing to and adds the size of one unit (an int is 2 bytes so stackptr+1 is offset of 2 bytes in memory)

Your last example:
       (*stackptr)[n];
Dereference the value pointed by stackptr and index the Nth element in the array (assuming stackptr is an array of pointers)

0
 
LVL 11

Expert Comment

by:Jase-Coder
ID: 12331506
TRY

int *arrayptr;

arrayptr = new int[50];

//access the nth element

for(int i=0; i<50; i++)
  cout << *(arrayptr + i );

just imagine the array has had data assigned to it.

if my solution seems the same as mongol then, sorry I never read his
0
 
LVL 3

Expert Comment

by:yasser_helmy
ID: 12332182
*(stackptr+n)
0
 

Author Comment

by:travishaberman
ID: 12333456
I will try these tonight about 8 hours from now and get back to you with the points.  Thanks all,

-TH
0
 

Author Comment

by:travishaberman
ID: 12336193
That was perfect!!!  I think you just saved me about 10 hours of troubles..  Sorry I can only give you the 5 bills.

thanks again,

-TH
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

746 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

16 Experts available now in Live!

Get 1:1 Help Now