1

I'm getting a subscripting compiler error when trying this:

int **arrays;
// allocate and initialize it....
int pos1 = 0;
int pos2 = 1;
int value = (*arrays[pos1])[pos2];

If the part in parentheses dereferences to a int* pointer, why would the array access not be legal?

3
  • the part in the parentheses will dereference to an int, not an int* Commented Feb 19, 2013 at 5:02
  • Why? How would I dereference it to int*? Commented Feb 19, 2013 at 5:03
  • arrays[pos1] gives you an int* and then you de-reference that int* using the operator *. So you get an int. Commented Feb 19, 2013 at 5:07

3 Answers 3

3

The part in parentheses is an int. Firstly, arrays[pos1] yields a pointer (int*), that you then have dereferenced (via the pre-fixed * operator). Thus, (*arrays[pos1]) is a simple int, and not a pointer.

If your goal is simply to access a position with in a multi-dimensional array, forget the * and just use:

arrays[pos1][pos2]
Sign up to request clarification or add additional context in comments.

Comments

2

The trouble is in the below line:

int value = (*arrays[pos1])[pos2];

You have an int **, arrays[pos1] makes it an int *, which you are further dereferencing by doing *arrays[pos1], so when you do (*arrays[pos1])[pos2], (*arrays[pos1]) is an int and not int* which causes the error.

You can just do

int value = arrays[pos1][pos2];

Comments

0

A way to put a value (gradient) and then trace sumlines. To get the array to behave, is a real excercise with brackets. Maybe hirarchical ordered. Being of

FOR loops with structural symbol { }, containing math result symbol [ ][ ]. It is the FOR loops that generate the array Display !

The margins are relatively small, to obtain a decent setup or complete gibberish. The objective is to have information stored in a 2D array. Put new information in there, and get access to that information slots later. Im trying to have some fun with it, and see how the logic beast responds. Its said that in the range 1000 hours of training, you should get a feeling for it :)

   #include <stdio.h>
   int main()
   //2D_Array. Multilist. Sumline and Total Sum .
   //Select each coordinate by pressing ENTER after each number .
   //KHO2016.no7. mingw (TDM-GCC-32) . c-ansi .
   {
   //declare, valuate
   int a,b,c=1,d=1,i,j,k,l,sum0=0;
   int ar_a[20][20];

   //calculate
   jump0:
   for (l=0;l<1;l++)                // vary the value l<1 - l<10
   {printf ("M.M.M Shelf %d\n",l); // SHELF2
   for (k=0;k<1;k++)              // SHELF1
   {printf ("SumLine%d = %d\n",k,sum0);
   {for (i=1;i<6;i++)           // COLUMS .
   for (j=0;j<1;j++)           // LINES . per COLUM 
   {ar_a[i][j]=d*c++;         // put value into 2D_array indevidual result slot.
   sum0=sum0+(ar_a[i][j]);
   printf ("%d%d(%.2d*%.2d=%.3d)\t",i,j,d,c-1,ar_a[i][j]);}}}}

   printf ("TOTAL SUM = %d . Select 2 coordinates [1-5] enter, [0] enter: \n",sum0);
   scanf ("%d%d",&a,&b);
   printf ("You selected : %d and %d . Result value = %d\n",a,b,ar_a[a][b]);
   goto jump0;

   //terminate
   return 0;
   }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.