C 언어 포인터 변수와 배열 Feat. scanf()
포인터 변수 활용과 사용자 인풋값을 받아 배열에 접근하는 코드
#include <stdio.h>
int main()
{
int my_int_array[] = {10,20,30,40,50,60,70};
int *pint_value = &my_int_array[0];
int index = 0;
int count = sizeof(my_int_array)/sizeof(int);
printf("인덱스 값은(0~%d)? ",count-1);
scanf("%d", &index);
if ( index >= 0 && index < count ){
pint_value = pint_value + index;
printf("인덱스 %d 에 해당하는 값은 %d 입니다. \n", index, *pint_value);
}
else
{
printf("인덱스 값은 0 보다 크고, %d보다 작아야 합니다. \n", count);
}
return 0 ;
}
실행시 :
ndgndg91@LAPTOP-CCFK7MKV:~$ ./practice
인덱스 값은(0~6)? 2
인덱스 2 에 해당하는 값은 30 입니다.
ndgndg91@LAPTOP-CCFK7MKV:~$ ./practice
인덱스 값은(0~6)? 6
인덱스 6 에 해당하는 값은 70 입니다.
#include <stdio.h>
int main()
{
int my_int_array[] = {10,20,30,40,50,60,70};
int *pint_value = &my_int_array[0];
int index = 0;
int count = sizeof(my_int_array)/sizeof(int);
printf("인덱스 값은(0~%d)? ",count-1);
scanf("%d", &index);
if ( index >= 0 && index < count ){
pint_value = pint_value + index;
printf("인덱스 %d 에 해당하는 값은 %d 입니다. \n", index, *pint_value);
}
else
{
printf("인덱스 값은 0 보다 크고, %d보다 작아야 합니다. \n", count);
}
return 0 ;
}
실행시 :
ndgndg91@LAPTOP-CCFK7MKV:~$ ./practice
인덱스 값은(0~6)? 2
인덱스 2 에 해당하는 값은 30 입니다.
ndgndg91@LAPTOP-CCFK7MKV:~$ ./practice
인덱스 값은(0~6)? 6
인덱스 6 에 해당하는 값은 70 입니다.
댓글
댓글 쓰기