0

I'm trying to do pointer arithmetic with a pointer to array, but I get a wrong value since I can't dereference the pointer properly. Here is the code:

  #include "stdlib.h"
  #include "stdio.h"

  int main()
  {
  int a[] = {10, 12, 34};

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

  int (*b)[3] = &a;
  for (int i = 0; i < 3; ++i)
  {
      printf("%d", *(b++));
  }
 printf("\n");

  return 0;
  }

In the second for I can't get to print the correct value.

It doesn't work even if I write

printf("%d", *b[i]);

I'd like to see how to print correctly using the b++ and the b[i] syntax.

2
  • 1
    printf("%d", *(b++)); --> printf("%d", (*b)[i]); Commented May 12, 2015 at 21:55
  • Replacing ++i with i++ should help. Commented Feb 29, 2016 at 2:31

3 Answers 3

1

The following should work:

printf("%d\n", *( *b+i ));

// * b + i will give you each consecutive address starting at address of the first element a[0].
// The outer '*' will give you the value at that location.

instead of:

printf("%d", *(b++));
Sign up to request clarification or add additional context in comments.

3 Comments

I'll select this has the correct answer but I want more explanations, why this doesn't work, and what it's doing? printf("%d\n", *( *b++ )); And why this one works: printf("%d\n", *( *b)); b = *b + 1; with a warning: incompatible pointer types assigning to 'int (*)[3]' from 'int *
*b de-references 'b' i.e. it gives you what is at the address pointed to by 'b', say address location 1000. When you do a '+ 1' to it, you are pointing to the next element from location 1000. A *b++ will cause 'b' to be incremented i.e. the pointer's address itself will be incremented, not what the pointer is pointing to. Also ... for 1-Dimensional arrays, try using just int *b = a; and use int (*b)[3] = a; for 2-Dim arrays e.g. a[2][3].
when I write *b++ I'm incrementing b why? It is the same as b++? When I do b = *b + 1 I'm incrementing what b points to. To use the postfix ++ I should write: (*b)++, correct?
1

You have declared b to be a pointer to arrays of 3 integers and you have initialized it with address of a.

int (*b)[3] = &a;

In the first loop you will print the first element of a array but then you will move 3*sizeof(int) and trigger undefined behavior trying to print whatever there is.

To print it correctly:

int *b = a;
// int *b = &a[0];    // same thing
// int *b = (int*)&a; // same thing, &a[0] and &a both points to same address,
                      // though they are of different types: int* and int(*)[3]
                      // ...so incrementing they directly would be incorrect,
                      // but we take addresses as int* 
for (int i = 0; i < 3; ++i)
{
    printf("%d", (*b++));
}

3 Comments

Wouldn't you need int *b = a;? Or does C allow the conversion int *b = &a;?
I don't think they are both correct. Your compiler just happens not to emit an error. I get array_ptr2.c:9:10: error: incompatible pointer types initializing 'int *' with an expression of type 'int (*)[3]' [-Werror,-Wincompatible-pointer-types] with clang, compiled in C99 mode.
@juanchopanza True. int* b = a; and int *b = (int*)&a; are both correct, but not int *b = &a;
0

gcc will complain about the formatting in the second for loop: it will tell you format specifies type 'int' but the argument has type 'int *

your assignment of a to b should look like this:

int *b = a

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.