Skip to content

Instantly share code, notes, and snippets.

@krysseltillada
Created May 21, 2015 14:27
Show Gist options
  • Select an option

  • Save krysseltillada/b237ca4173f2eb2dc4fb to your computer and use it in GitHub Desktop.

Select an option

Save krysseltillada/b237ca4173f2eb2dc4fb to your computer and use it in GitHub Desktop.
ways of initializing arrays
#include <iostream>
int main()
{
const unsigned MAXNUM = 10;
/// initialize 10 int objects
int num[MAXNUM] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
/// declare 10 int array of pointers
int *ptr_num[MAXNUM];
for(unsigned i = 0; i < MAXNUM; i++)
ptr_num[i] = &num[i];
for(auto i2 : ptr_num)
std::cout << *i2 << std::endl;
int (*pptr_num)[MAXNUM] = &num; /// pptr_num is a pointer to an array of 10 integers
for(auto i6 : *pptr_num)
std::cout << i6 << std::endl;
int(&num_Ref)[MAXNUM] = num; /// num_ref is a reference to an array of 10 integers
for(auto i5 :num_Ref) {
std::cout << i5 << std::endl;
}
char *str[] = {"hello", "world", "fck"};
for(auto str_ : str)
std::cout << str_ << std::endl;
for(std::string s : str) /// str pointer is assign to string to get the element[0]
for(char c : s) /// for every character in string s
std::cout << c << std::endl;
std::string str_arr[] = {"lool", "hi", "hello", "nahh"}; /// array of strings
for(auto word : str_arr) /// word assumes str_arr as string
for(auto token : word) /// token assumes word as a char /// for every char token in a string word
std::cout << token << std::endl;
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment