int my_array[5]my_array[0] = 1; my_array[1] = 2; ...int my_array[] {1, 2, 3, 4, 5 }struct Rectangle
{
int length;
int width;
};
int main()
{
Rectangle rect_arr[10];
rect_arr[0].length = 1;
rect_arr[0].width = 2;
return 0;
}
{
JACKJACK, // = 0
DASH, // = 1
VIOLET, // = 2
NUM_STUDENTS, // = 3
};
int main()
{
int students_scores[NUM_STUDENTS];
students_scores[JACKJACK] = 0;
students_scores[DASH] =100;
return 0;
}
#include <iostream>
using namespace std;
void doSomething(int students_scores[])
{
cout << students_scores[1] << endl;
cout << students_scores[2] << endl;
}
int main() {
const int num_students = 20;
int students_scores[num_students] { 1, 2, 3, 4, 5, };
doSomething(students_scores);
return 0;
}
#include <iostream>
using namespace std;
int main() {
int scores[] = { 84, 92, 76, 81, 56};
const int num_students = sizeof(scores) / sizeof(int);
int max_score = 0;
int total_score = 0;
for(int i = 0; i < num_students; ++i)
{
total_score += scores[i];
max_score = (max_score < scores[i]) ? scores[i] : max_score;
}
double avg_score = static_cast<double>(total_score) / num_students;
return 0;
}
#include <iostream>
using namespace std;
void printArray(const int array[], const int length)
{
for (int index = 0; index < length; ++index)
{
cout << array[index] << " ";
}
cout << endl;
}
int main()
{
const int length = 5;
int array[length] = { 3, 5, 2, 1, 4};
printArray(array, length);
for(int startIndex = 0; startIndex < length -1; ++startIndex)
{
int smallest = startIndex;
for(int currentIndex = startIndex + 1; currentIndex < length; ++ currentIndex)
{
if(array[smallest] > array[currentIndex])
{
smallest = currentIndex;
}
}
// swap two values in the array
// std::swap(array[smallistIndex], array[startIndex]);
{
int temp = array[smallest];
array[smallest] = array[startIndex];
array[startIndex] = temp;
}
printArray(array, length);
}
return 0;
}
int main()
{
const int length = 5;
int array[length] = { 3, 5, 2, 1, 4};
printArray(array, length);
for(int i = length - 1 ; i > 0 ; i--)
{
for(int j = 0; j < i; j++)
{
int temp;
if(array[j] > array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printArray(array, length);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
const int num_rows = 3;
const int num_columns = 5;
int array[][num_columns] // =
{
{1, 2, },
{2, 3, 4, 5, 6},
{7, 8, 9, 10, 11}
};
for(int row = 0; row < num_rows; ++row)
{
for(int col = 0; col < num_columns; ++col)
{
//cout << '[' << row << ']' << '[' << col << ']' << '\t';
cout << array[row][col] << '\t';
}
cout << endl;
}
cout << endl;
return 0;
}
만들어서올려라
#include <iostream>
using namespace std;
int main()
{
int x = 5;
double d = 1.0;
int *ptr_x = &x;
double *ptr_d = &d;
cout << "x : " << x << "\tptr_x : " << *ptr_x << endl;
cout << "d : " << d << "\tptr_d : " << *ptr_d << endl;
cout << "x의 주소 : " << &x << "\t*ptr_x : " << ptr_x << endl;
cout << "d의 주소 : " << &d << "\t*ptr_d : " << ptr_d << endl;
cout << "int 자료형의 크기 : " << sizeof(x) << endl;
cout << "double 자료형의 크기 : " << sizeof(d) << endl;
cout << "int 자료형의 포인터 변수 크기 : " << sizeof(ptr_x) << endl;
cout << "double 자료형의 포인터 변수 크기 : " << sizeof(ptr_d) << endl;
return 0;
}
x : 5 ptr_x : 5
d : 1 ptr_d : 1
x의 주소 : 0x7ffeefbff468 ptr_x : 0x7ffeefbff468
d의 주소 : 0x7ffeefbff468 ptr_d : 0x7ffeefbff460
int 자료형의 크기 : 4
double 자료형의 크기 : 8
int 자료형의 포인터 변수 크기 : 8
double 자료형의 포인터 변수 크기 : 8
#include <iostream>
void doSomething(double *ptr)
{
if (ptr != nullptr)
{
// do sommethin useful
std::cout << *ptr << std::endl;
} else
{
// do nothing wit ptr
std::cout << "Null ptr, do no thing" << std::endl;
}
}
int main(int argc, const char * argv[]) {
double *ptr{ nullptr }; //modern c++
doSomething(ptr);
doSomething(nullptr);
double d = 123.4;
doSomething(&d);
ptr = &d;
doSomething(ptr);
return 0;
}
#include <iostream>
int main()
{
using namespace std;
int array[5] = { 9, 7, 5, 3, 1 };
cout << array[0] << " " << array[1] << endl;
cout << array << endl;
cout << &array[0] << endl;
cout << &array[1] << endl;
cout << *array << endl;
int *ptr = array;
cout << ptr << endl;
cout << *ptr << " " << *(ptr + 1) << endl;
cout << sizeof(array) << endl;
cout << sizeof(ptr) << endl;
return 0;
}
9 7
0x7ffeefbff430
0x7ffeefbff430
0x7ffeefbff434
9
0x7ffeefbff430
9 7
20
8✓ 자료형 크기는 다름을 확인 할 수 있다.
#include <iostream>
using namespace std;
int main()
{
int array[] = { 9, 7, 5, 3, 1};
int *ptr = array;
for(int i = 0; i < 5; ++i)
{
//cout << array[i] << " " << (uintptr_t)&array[i] << endl;
cout << *(ptr+i) << " " <<(uintptr_t)(ptr + i) << endl;
}
char name[] = "Jack Jack";
const int n_name = sizeof(name) / sizeof(name[0]);
for(int i = 0; i < n_name ; ++i)
{
cout << *(name + i) ;
}
return 0;
}
new int로 선언을 하면, 4byte만큼 할당하여 사용할 수 있는 주소를 할당해줌 -> 포인터로 받아야 함.new (std::nothrow) int ~~ 이런식으로 선언해주면 좋다.delete 로 메모리를 반환을 해줘야함.delete 만 하면 사용했던 주소는 그대로 나오고 쓰레기 값이 저장되어 있으므로 nullptr 을 해주면 좋다.#include <iostream>
using namespace std;
int main()
{
//int var
//var = 7;
int *ptr = new (std::nothrow) int { 7 };
if(ptr)
{
cout << ptr << endl;
cout << *ptr << endl;
} else
{
cout << "Could not allocate memory" << endl;
}
delete ptr;
ptr = nullptr;
cout << "After delete" << endl;
if (ptr != nullptr)
{
cout << ptr << endl;
cout << *ptr << endl;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int length;
cin >> length;
// int array[length]
int *array = new int[length] { 11, 22, 33, 44, 55, 66};
array[0] = 1;
array[1] = 2;
for(int i = 0; i < length; i++)
{
cout << array[i] << endl;
cout << (uintptr_t)&array[i] << endl;
}
delete [] array;
return 0;
}
int value = 5; int *ptr = &value; 일 때 *ptr = 6; 으로 value 값 변경가능const) 선언 되었을 때 포인터 변수를 사용하려면 포인터 변수도 const 로 선언해야한다. int value = 5;
const int *ptr = &value;
//*ptr = 6;
value = 6;
cout << *ptr << endl;
이 때는 6이 출력된다.
*ptr = 6;으로는 값을 변경할 수 없다.*ptr 앞에 const는 가리키고 있는 주소에 값을 안바꾼다는 의미이다. ptr이 가지고 있는 주소 값을 안바꾼다는 것은 아니다.
따라서 아래 예제가 가능하다.
int value1 = 5;
const int *ptr = &value1;
int value2 = 6;
ptr = &value2;
6 이 출력된다.
위에 사례를 막고 싶다면 const 앞에 *를 붙인다.
- 포인터 변수로써 갖고 있는 값은 못바꾼다는 표현 -
int value = 5;
int *const ptr = &value;
*ptr = 10;
int value2 = 8;
// 이렇게는 사용 불가
ptr = &value2;
int value = 5;
const int *ptr1 = &value;
int *const ptr2 = &value;
const int *const ptr3 = &value;
조금 더 편하게 사용하기 위해서는 참조에서 다룬다.