3

I'm trying to iterate over an array of structs by pointer arithmetic. However it gives me results I can't understand.

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    int hours;
    int minutes;
    int seconds;
} Time;

void time_print(Time t)
{
    printf("Time is: %d:%d:%d\n", t.hours, t.minutes, t.seconds);
}

int main(void)
{    
    Time *testTimePointers[2];

    testTimePointers[0] = malloc(sizeof(Time));
    testTimePointers[0]->hours = 11;
    testTimePointers[0]->minutes = 10;
    testTimePointers[0]->seconds = 9;

    testTimePointers[1] = malloc(sizeof(Time));
    testTimePointers[1]->hours = 7;
    testTimePointers[1]->minutes = 6;
    testTimePointers[1]->seconds = 5;

    time_print(*(testTimePointers[0]));
    time_print(*(testTimePointers[1]));

    printf("=============\n");


    Time *ttp_cur = NULL;
    ttp_cur = testTimePointers[0];

    time_print(*(ttp_cur));

    ttp_cur++;

    time_print(*(ttp_cur));


    free(testTimePointers[0]);
    free(testTimePointers[1]);

    return 0;
}

Results in

Time is: 11:10:9 
Time is: 7:6:5
=============
Time is: 11:10:9
Time is: 0:0:0

In the second block, the second line should be Time is: 7:6:5 instead of Time is: 0:0:0.

1
  • 1
    After ttp_cur++; pointer makes step equal to sizeof(Time) and points to nowhere. Commented Apr 13, 2015 at 12:00

2 Answers 2

7

The problem is that you are trying to increment a pointer ttp_cur that does not point to an array.

The value you are trying to increment, ttp_cur, came from testTimePointers[0], which in turn came from malloc of a single struct Time, not an array. That's why when you increment that pointer, the behavior is undefined.

It would be OK to increment a pointer pointing to testTimePointers array, however. If you did this

Time **ttp_cur_p = testTimePointers;
time_print(*(*ttp_cur_p));
ttp_cur_p++;
time_print(*(*ttp_cur_p));

you would get the results that you expect (demo).

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

Comments

5

I think that in the second part of the program you mean the following

    //...

    Time **ttp_cur = testTimePointers;

    time_print(**ttp_cur );

    ttp_cur++;

    time_print(**ttp_cur);


    free(testTimePointers[0]);
    free(testTimePointers[1]);

    return 0;
}

If you have an array like

T a[2];

where T is some type specifier then the pointer to its first element is declared the following way

T *p = a;

and expression

++p;

will point to the second element of the array.

Now if to consider your array

Time *testTimePointers[2];

then type specifier T will be equal to Time *. So the declaration of the pointer to the first element of the array will look like

Time **ttp_cur = testTimePointers;

and expression

++ttp_cur;

will point to the second element of the array.

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.