코딩/C언어

포인터 와 배열 문제 (6~10)

tree0505 2025. 7. 9. 11:25
반응형
    • _21_문제

  • 문제023_포인터와배열06.cpp
#include <stdio.h>

int main(void) {

	int num[4] = {1, 2, 3, 4};
	int*pt = num; //num[0],num[1],num[2],num[3];
				  //&num[0],&num[1],
	
	//int* pt = & num[0];
	//int* pt = num; //축약형 
	//위의 2개는 같다.  
	
	//int* pt = &num[1]; 
	//int* pt = num + 1; 
	//위의 2개도 같은것 

	//0의 주소 x1 

	pt++; // x5 

	*pt++ = 5; //1, 5, 3, 5 //pt = x9 

	*pt++ = 10; //1,5,10,4 //pt = x13 

	pt --; //pt = x9

	*pt++ += 20; //1,5,30,4 // pt = x13 
	printf("%d, %d, %d %d\n", num[0], num[1], num[2], num[3]);
		
	return 0;
}

  • 문제023_포인터와배열07.cpp
#include <stdio.h>

int main(void) {

	int array[] = {100, 200, 300, 400, 500};
	int * ptr;
	ptr = array; //&array[0];의 주소를 준것을 축약형 //x1 
	printf("%d\n", *(ptr + 3) + 100);
	//ptr + 3 => x13 => *x13 => 400 + 100 => 500 
		
	return 0;
}

  • 문제023_포인터와배열08.cpp
#include <stdio.h>

int main(void) {

	int array[] = {10, 11, 12, 13, 14};
	int * p;
	p = array; //*&array[0]; 의 주소 // x1 

	printf("%d\n", array[1]); 	// 11

	printf("%d\n", *p + 1);  	// 11
	//*이 먼저  // 11 

	printf("%d\n", *array + 1); // 11

	printf("%d\n", *p++); 		// 10
		
	return 0;
}

  • 문제023_포인터와배열09.cpp
#include <stdio.h>

int main(void) {

	int code[] = {10, 20, 30, 0};
	int*p = code; //
	
	printf("%d\n" , ++*p); //*p => 10, ++ => 11 
	//{11, 20, 30, 0}; 

	printf("%d\n" , *p++); //11 , p = x5 
	//출력이 되고 ++ 

	printf("%d\n" , *++p); // x9 , 30 

	printf("%d\n" , (*p)++); //30, {11, 20, 31, 0}; 

	printf("%d\n" , *(p++)); // 31 , x9 => x13 
	printf("%d\n" , *p); // x13 => 0

	//11, 11, 30, 30, 31 , 0
	return 0;
}

  • 문제023_포인터와배열10.cpp
#include <stdio.h>
#include <stdlib.h> // srand();
#include <time.h> // time(NULL)

int main(void) {
	
	int ary[3]; // 0 , 0 , 0
	int s = 0;
	int i = 0;

	*(ary + 0) = 1; // {1,0,0}

	ary[1] = *(ary + 0) + 2; //{1,3,0}

	ary[2] = *ary + 3; //{1,3,4}
	
	for(i = 0; i < 3; i++){
		s = s + ary[i]; //8
	}
	
	printf("%d\n" , s);
	 


	return 0;
}
반응형