1

I have a pointers in C, and I can't figure out how it works. Below is the code:

// ptr points to an array of 2 ints
int (*ptr)[2];
int torf[2][2] = {12, 14, 16};
ptr = torf;

int (*ptr_2)[2];
int torf_2[2][2] = { {12}, {14, 16}};
ptr_2 = torf_2;
  
printf("%d %d\n", **ptr, **(ptr + 2));
printf("%d %d\n", **ptr_2, **(ptr_2 + 2));

The answer I want should be:

12 16
12 14

But actually I got on my PC:

12 6422000
12 12

Any ideas?

3
  • 3
    There should be several relevant warnings from your compiler. If your compiler isn’t giving you any warnings, you should look up how to turn them on. Commented Oct 30, 2020 at 5:50
  • Remember that for any pointer or array ptr and index i the expression *(ptr + i) is equal to ptr[i]. That means **(ptr + 2) in your code is equal to *ptr[2], which is out of bounds. Commented Oct 30, 2020 at 6:31
  • The problem is **(ptr + 2) - who'd know what it means. Just use the bracket notation with pointers. That stands for *ptr[2] which is much easier to make sense of. Commented Oct 30, 2020 at 8:14

2 Answers 2

2

try this:

// ptr points to an array of 2 ints
int(*ptr)[2];
int torf[2][2] = { 12, 14, 16 };
ptr = torf;

int(*ptr_2)[2];
int torf_2[2][2] = { {12}, {14, 16} };
ptr_2 = torf_2;

printf("%d %d\n", **ptr, **(ptr + 1));
printf("%d %d\n", **ptr_2, **(ptr_2+1));

when you use pointers it is like the indexes of the array it begin from 0;

good work

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

Comments

2

If you add these lines to the end of your program:

printf("%p: %p %p\n", ptr, ptr+1, ptr+2);
printf("%p: %p %p\n", *ptr, *ptr+1, *ptr+2);
printf("%p: %p %p\n", **ptr, **ptr+1, **ptr+2);

You will note that in the first case, the numbers increase by 8, which could either be the size of a pointer or two ints. Same goes for the second, but the third increases by the size of an int; so that is good.

So, to disambiguate 2 ints or 1 address, lets do a s/2/3/g. Now, we see the first case the increment is now 12 ( = 3 * 4 ). Your second case ( *ptr + i ) increments by 4, so is the address of successive ints Your third case is the integer values themselves.

Where did it get confusing? Quick checklist:

  1. When you are trying to workout pointer / indexing problems, use unique values as much as possible.

  2. Pay attention when the compiler warns you. Eventually you will know to ignore "format '%p' expects argument ... ", but it takes time to build the confidence.

  3. There is a handy program, cdecl, which converts C type expressions into something english like.

  4. In virtually all C implementations, int x[2][2], y[4]; have the same layout; that is, C multi-dimensional arrays are just an overlay of a single dimension array, with the arithmetic cleverly hidden. For this reason, definitions like int (*p)[2]; are rare and rarely useful. If you must go down this road, it is likely better to do something like:

    typedef int pair[2]; pair torf[2] = { { 0, 1 }, { 2, 3 }}; pair *ptr = torf;

if nothing else, somebody has a chance to understand it...

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.