Created
September 17, 2017 03:25
-
-
Save AnastasiaDunbar/3bfe176f52d5f920f2a3fa375275bd13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// - - Hello World - - | |
#include <iostream> //For input and output. | |
using namespace std; //"std::cout" into "cout". | |
int main(){ | |
cout<<"Hello, world!\n"; | |
return 0; | |
} | |
/*C version: | |
#include "stdio.h" | |
int main(int argc,char*argv[]){ | |
printf("Hello, world!"); | |
return 0; | |
} | |
*/ | |
// - - Input and output strings - - | |
#include <iostream> | |
#include <string> | |
using namespace std; | |
int main(){ | |
string input; | |
cout<<"Enter: "; | |
//cin>>input; //"cin" extraction always considers spaces as terminating. | |
getline(cin,input); | |
cout<<"Your input: "<<input<<endl; | |
cout<<"Character codes: "; | |
for(int i=0;i<input.length();i++){ | |
cout<<hex<<(int)input[i]; | |
if(i!=input.length()-1){cout<<", ";} | |
} | |
return 0; | |
} | |
// - - Pointers - - | |
#include <iostream> | |
using namespace std; | |
/* | |
"&" is the address-of operator, and can be read simply as "address of", | |
"*" is the dereference operator, and can be read as "value pointed to by". | |
*/ | |
int main(){ | |
//Example 1: | |
int foo=53; | |
cout<<"foo: "<<foo<<"; &foo: "<<&foo<<endl; | |
int*bar; cout<<"int* bar;"<<endl; | |
cout<<"bar: "<<bar<<"; &bar: "<<&bar<<endl; | |
bar=&foo; cout<<"bar = &foo; (bar points at foo's address)"<<endl; | |
cout<<"bar: "<<bar<<"; *bar: "<<*bar<<"; &bar: "<<&bar<<endl; | |
//Example 2: | |
char myValue[]="Hello!"; | |
//Store the address as an unsigned integer type and has exactly the size of a pointer. | |
uintptr_t address=reinterpret_cast<uintptr_t>(&myValue); | |
//Note that in C and C++, single quotes identify a single character, while double quotes create a string literal. | |
cout<<"\nThe value of address "<<&myValue<<" is \""<<(char*)myValue<<"\"."; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment