12345678910111213141516171819202122232425262728293031323334
#include <iostream> #include <string> #include <algorithm> #include <random> using namespace std; const int DeckSize = 55; void init(int *cards) { for (int n = 1, i = 0; n <= 10; ++n) for (int j = 0; j < n; ++j) cards[i++] = n; } void shuffle(int *cards) { static default_random_engine rnd(random_device{}()); shuffle(cards, cards + DeckSize, rnd); } void print(const int *cards) { for (int i = 0; i < DeckSize; ++i) cout << cards[i] << ' '; cout << '\n'; } int main() { int cards[DeckSize]; init(cards); print(cards); shuffle(cards); print(cards); }
int deck[DeckSize]; // DeckSize must be const (in standard C++)
12
const char* str = "hello world"; // str[2] = 'x'; // illegal statement; probably segfault if run
1234
// This function promises not to modify the ints that deck points to. void print(const int* deck) { // ... }