Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdlib.h>
- #include <stdio.h>
- #include <malloc.h>
- using namespace std;
- int main () {
- int *pointer; //Pointer
- int value; // Variable to store user input
- pointer = (int*) malloc(5*sizeof(int)); //Initiate malloc
- if(!pointer)
- {
- cout << "Memory Allocation Failed"; //Error handling
- exit(1);
- }
- cout << "Please input values!" << endl << endl; // User input values
- for (int i=0; i<5; i++) // User must input 5 values, but this can be changed as desired
- {
- cin >> value;
- pointer[i] = value; // Value is stored
- }
- cout << "Your values are..." << endl;
- for (int i=0; i<5; i++)
- {
- cout << *(pointer+i) << endl; // Output stored values
- }
- free(pointer); //Free the memory
- return 0;
- }
RAW Paste Data