Shreyash Wankhede
Shreyash Wankhede

Reputation: 1

why this piece of code prints only 1 and not the entire elements in an array?

I simply declared a 1D array along with a pointer that is pointing to the whole array. I then implemented a for loop expecting to print all the elements in my array. But the for loop is printing only 1. I need to know why it is not printing all the elements of the particular array.Here's is the following piece of code:

#include <stdio.h>
int main() {
  int array[5] = {1,2,3,4,5}, (*pointer)[5] = &array;
  for(*pointer; *pointer <= (array + 4); *pointer++) {
    printf("%d", **pointer);
  }
  return 0;
}

Upvotes: 0

Views: 80

Answers (3)

H.S.
H.S.

Reputation: 12669

Remember - when you increment a pointer, it gets incremented in steps of the object size that the pointer can point to.

The type of pointer is int (*)[5] i.e. pointer to an array of 5 integers, which means, pointer can point to any array of type int [5]. This expression *pointer++ in for loop will be grouped as - *(pointer++) [check operator precedence table] i.e. the pointer will be first incremented by size of object of type int [5] and then the unary * operator will be applied to it. pointer increment will result in pointer point to one past element of array array [which is fine as per language standard].

 pointer
     |
    _____________________
    | 1 | 2 | 3 | 4 | 5 |
    ---------------------

after pointer++ :
                        pointer
                         |
    _________________________
    | 1 | 2 | 3 | 4 | 5 |
    -------------------------

So, after incrementing pointer, the for loop condition *pointer <= (array + 4) will result in false and loop exits after printing first element of array array.

You can access the members of array using pointer pointer in this way - (*pointer)[i], where i is the ith member of array array. Below program demonstrate the ways to print the array members:

#include <stdio.h>

int main (void) {
    int array[5] = {1,2,3,4,5}, (*pointer)[5] = &array;

    // print array members using pointer 'pointer'
    for (size_t i = 0; i < sizeof (*pointer)/sizeof (*pointer)[0]; ++i) {
        printf ("%d ", (*pointer)[i]);
    }
    printf ("\n");

    // using a temporary pointer
    for (int *tmp = *pointer; tmp < (*pointer) + sizeof (*pointer)/sizeof (*pointer)[0]; ++tmp) {
        printf("%d ", *tmp);
    }
    printf ("\n");

    // the above is same as this
    for (int *tmp = array; tmp < array + sizeof(array) / sizeof(*array); ++tmp) {
        printf("%d ", *tmp);
    }
    printf ("\n");

    // another way (check the loop condition)
    for(int *tmp = *pointer; tmp < *(pointer + 1); ++tmp) {
        printf("%d ", *tmp);
    }
    printf ("\n");

    return 0;
}

One more thing, when you are compiling your program, the compiler must be throwing warning message

warning: expression result unused [-Wunused-value]

on init-clause and iteration-expression of for loop:

for(*pointer; *pointer <= (array + 4); *pointer++) 
    ^^^^^^^^                           ^^^^^^^^

the reason is -
init-clause expression *pointer will result in int [5] which will be converted to pointer to first element of array array and to which you are doing nothing and
iteration-expression is also doing the same because, when unary * operator applied to pointer++ expression result, it will also result in int [5] which will be converted to pointer to first element of array array and to which you are doing nothing. Hence, they are resulting in warnings.

Upvotes: 0

Zakk
Zakk

Reputation: 2063

If you want to use a pointer to print an array:

#include <stdio.h>

int main()
{
    int array[] = {1, 2, 3, 4, 5};
    const int size = sizeof(array) / sizeof(*array);
    
    int *pointer;
    for(pointer = array; pointer < array + size; pointer++)
        printf("%d ", *pointer);
}

Upvotes: 1

ShadowRanger
ShadowRanger

Reputation: 155363

pointer is a pointer to an int[5] array. Incrementing it by one moves it past the entirety of array in one fell swoop (it's equivalent to adding 5 to an int*), while array + 4 (which resolves array to a pointer to int), only adds four ints to it, not four int[5]'s worth of space, so you only print the first element, then the loop terminates (array + 4 is one int before pointer + 1's address).

If you want pointer to point to just one element, don't declare it with (*pointer)[5], use plain int*s; there's little benefit to making it a pointer to array here.

Upvotes: 4

Related Questions