陣列 指標 2

 avatar
user_6817964
c_cpp
2 years ago
436 B
1
Indexable
Never
#include <stdio.h>

int main()
{
    int a[5] = { 1, 2, 3, 4, 5 };
    
    int *ptr = a;
    for (int i = 0; i < 2; i++) {
        ptr[i] += 3;
    }
    for (int i = 0; i < 5; i++)
        printf("a[%d] = %d\n", i, a[i]);
    printf("\n");

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

}