0

If I have the following code in a function:

int A[5][5];
  int i; int j;
  for(i=0;i<5;i++){
    for(j=0;j<5;j++){
      A[i][j]=i+j;
      printf("%d\n", A[i][j]);
    }
  }

This simply prints out the sum of each index. What I want to know is if it's possible to access each index in the static array in a similar fashion to dynamic array. So for example, if I wanted to access A[2][2], can I say:

*(A+(2*5+2)*sizeof(int))?

I want to perform some matrix operations on statically allocated matrices and I feel like the method used to dereference dynamic matrices would work the best for my purposes. Any ideas? Thank you.

4
  • 1
    That array is not static as you claim. Commented Feb 28, 2012 at 20:44
  • The multiplication by sizeof() is unnecessary if the pointer being dereferenced is a pointer-to-int. Pointer arithmetic steps by the size of the type pointed to pointed. Commented Feb 28, 2012 at 20:48
  • 1
    @dmckee not only is it not necessary, it's wrong. First, it won't do what he's expecting it does, second, you can go over the bound of the array and get into undefined behavior. Commented Feb 28, 2012 at 20:50
  • To @EdS.'s point the array is "automatic" or "at file scope" depending on the existence or non-existence of a un-exhibited enclosing function. Commented Feb 28, 2012 at 20:50

4 Answers 4

4

That's the way to do it: A[i][j].

It prints out the sum of the indexes because, well, you set the element A[i][j] to the sum of the indexes: A[i][j] = i+j.

Sign up to request clarification or add additional context in comments.

3 Comments

I think the OP is asking how to access an element using pointer arithmetic. It's more a question about the way the data is allocated than about the "proper" way to access it.
@AdamLiss could be, or maybe he's confused that he's getting the sum of the indexes.
@LuchianGrigore: I read the statement, "This simply prints out the sum of each index" a bit differently from how you read it. You read it as, "The problem is, this is supposed to print out the array elements, but instead, it prints out the sums of their indices." I read it as, "This is just simple code to show what I mean; it prints out the array elements, which are set to the sums of their indices."
1

You can use:

*(*(A + 2) + 2)

for A[2][2]. Pointer arithmetics is done in unit of the pointed type not in unit of char.

Of course, the preferred way is to use A[2][2] in your program.

2 Comments

@LuchianGrigore I assumed the OP asked the equivalence of A[2][2] with explicit pointer arithmetic.
@LuchianGrigore A[2][2] its not wrong. But he's asking a way to access the elements of A using its pointer value.
1

The subscript operation a[i] is defined as *(a + i) - you compute an offset of i elements (not bytes) from a and then dereference the result. For a 2D array, you just apply that definition recursively:

a[i][j] == *(a[i] + j) == *(*(a + i) + j)

If the array is allocated contiguously, you could also just write *(a + i * rows + j).

When doing pointer arithmetic, the size of the base type is taken into account. Given a pointer

T *p;

the expression p + 1 will evaluate to the address of the next object of type T, which is sizeof T bytes after p.

Note that using pointer arithmetic may not be any faster than using the subscript operator (code up both versions and run them through a profiler to be sure). It will definitely be less readable.

Comments

0

Pointer arithmetic can be tricky. You are on the right track, however there are some differences between pointer and normal arithmetic. For example consider this code

int I = 0;
float F = 0;
double D = 0;
int* PI = 0;
float* PF = 0;
double* PD = 0;

cout<<I<<" "<<F<<" "<<D<<" "<<PI<<" "<<PF<<" "<<PD<<endl;
I++;F++;D++;PI++;PF++,PD++;

cout<<I<<" "<<F<<" "<<D<<" "<<PI<<" "<<PF<<" "<<PD<<endl;
cout<<I<<" "<<F<<" "<<D<<" "<<(int)PI<<" "<<(int)PF<<" "<<(int)PD<<endl;

If you run it see the output you would see would look something like this (depending on your architecture and compiler)

0 0 0 0 0 0
1 1 1 0x4 0x4 0x8
1 1 1 4 4 8

As you can see the pointer arithmetic is handled depending on the type of the variable it points to.

So keep in mind which type of variable you are accessing when working with pointer arithmetic.

Just for the sake of example consider this code too:

void* V = 0;

int* IV = (int*)V;
float* FV = (float*)V;
double* DV = (double*)V;

IV++;FV++;DV++;
cout<<IV<<" "<<FV<<" "<<DV<<endl;

You will get the output (again depending on your architecture and compiler)

0x4 0x4 0x8

Remember that the code snippets above are just for demonstration purposes. There are a lot of things NOT to use from here.

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.