#include <stdio.h>
int main()
{
//change the vaule by pointer
int i = 10;
int *p;
p = &i;
*p = 8;
//i must be 8
printf("*p = 8 // i = %d\n", i);
//reduce the vaule using (*p)-- ,value is changed after printing
(*p)--;
//i must be 7
printf("(*p)-- // i = %d\n\n", i);
//check the index
int a1[10] = { 0 };
int *p1;
p1 = a1;
printf("before *p1++ = %d\n" ,p1);
*p1++ = 1;
printf("*p1++ = 1\n");
for (int j = 0; j < 10; j++) {
printf("a1[%d] vaule = %d\n",j, a1[j]);
}
printf("after *p1++ = %d\n\n" ,p1);
//pointer priority is higher than ++
/*
1. pointer addree ++
(*p1++)
*/
int a2[10] = { 0 };
int *p2;
p2 = a2;
printf("before *++p2 = %d\n" ,p2);
*++p2 = 1;
printf("*++p2 = 1\n" );
for (int j = 0; j < 10; j++) {
printf("a2[%d] vaule = %d\n", j, a2[j]);
}
printf("after *++p2 = %d\n\n" ,p2);
/*1. p is changed to a[1] = p++
2. *p is changed to 1
++ operates twice
(*++p2) = 1 is equal to p2 = P2 + 1 and (*p2) == (*p2) + 1
*/
int a3[10] = { 0 };
int *p3;
p3 = a3;
printf("before (*p3)++ = %d\n" ,p3);
(*p3)++;
for (int j = 0; j < 10; j++) {
printf("a3[%d] vaule = %d\n", j, a3[j]);
}
printf("after (*p3)++ = %d\n\n" ,p3);
/* print *p3 and increase the pointer value*/
int a4[10] = { 0 };
int *p4;
p4 = a4;
printf("before ++*(p4+1) = %d\n" ,p4);
++*(p4+1);
for (int j = 0; j < 10; j++) {
printf("a4[%d] vaule = %d\n", j, a4[j]);
}
printf("after ++*(p4+1) = %d\n\n" ,p4);
/*1.p is added 1 size of int
2.a4[1]'s vaule increase to 1
3. nochange for pointer value
++*(p4+1) is equal to p4 + 1 and a4[1] = a4[1] + 1
*/
int a5[10] = { 0 };
int *p5;
p5 = a5;
printf("before (*(p5+1))++ = %d\n" ,p5);
(*(p5+1))++;
for (int j = 0; j < 10; j++) {
printf("a5[%d] vaule = %d\n", j, a5[j]);
}
printf("after (*(p5+1))++ = %d\n\n" ,p5);
/* a5[1] = 1
1.p5 is added 4 bit temperaily in this line => p5 = a5[1]
2. *p5 is added 1 => a5[1] = 1;
3. p5 doesn't changed
this line, we just got a access to p5 + 1 but didn't changed vaule of a5
*/
}
//어렵네 증말
int main()
{
//change the vaule by pointer
int i = 10;
int *p;
p = &i;
*p = 8;
//i must be 8
printf("*p = 8 // i = %d\n", i);
//reduce the vaule using (*p)-- ,value is changed after printing
(*p)--;
//i must be 7
printf("(*p)-- // i = %d\n\n", i);
//check the index
int a1[10] = { 0 };
int *p1;
p1 = a1;
printf("before *p1++ = %d\n" ,p1);
*p1++ = 1;
printf("*p1++ = 1\n");
for (int j = 0; j < 10; j++) {
printf("a1[%d] vaule = %d\n",j, a1[j]);
}
printf("after *p1++ = %d\n\n" ,p1);
//pointer priority is higher than ++
/*
1. pointer addree ++
(*p1++)
*/
int a2[10] = { 0 };
int *p2;
p2 = a2;
printf("before *++p2 = %d\n" ,p2);
*++p2 = 1;
printf("*++p2 = 1\n" );
for (int j = 0; j < 10; j++) {
printf("a2[%d] vaule = %d\n", j, a2[j]);
}
printf("after *++p2 = %d\n\n" ,p2);
/*1. p is changed to a[1] = p++
2. *p is changed to 1
++ operates twice
(*++p2) = 1 is equal to p2 = P2 + 1 and (*p2) == (*p2) + 1
*/
int a3[10] = { 0 };
int *p3;
p3 = a3;
printf("before (*p3)++ = %d\n" ,p3);
(*p3)++;
for (int j = 0; j < 10; j++) {
printf("a3[%d] vaule = %d\n", j, a3[j]);
}
printf("after (*p3)++ = %d\n\n" ,p3);
/* print *p3 and increase the pointer value*/
int a4[10] = { 0 };
int *p4;
p4 = a4;
printf("before ++*(p4+1) = %d\n" ,p4);
++*(p4+1);
for (int j = 0; j < 10; j++) {
printf("a4[%d] vaule = %d\n", j, a4[j]);
}
printf("after ++*(p4+1) = %d\n\n" ,p4);
/*1.p is added 1 size of int
2.a4[1]'s vaule increase to 1
3. nochange for pointer value
++*(p4+1) is equal to p4 + 1 and a4[1] = a4[1] + 1
*/
int a5[10] = { 0 };
int *p5;
p5 = a5;
printf("before (*(p5+1))++ = %d\n" ,p5);
(*(p5+1))++;
for (int j = 0; j < 10; j++) {
printf("a5[%d] vaule = %d\n", j, a5[j]);
}
printf("after (*(p5+1))++ = %d\n\n" ,p5);
/* a5[1] = 1
1.p5 is added 4 bit temperaily in this line => p5 = a5[1]
2. *p5 is added 1 => a5[1] = 1;
3. p5 doesn't changed
this line, we just got a access to p5 + 1 but didn't changed vaule of a5
*/
}
//어렵네 증말
댓글
댓글 쓰기