Answers for "using pointers in structurea c++"

C++
4

c++ pointers

#include <iostream>

using namespace std;
// isualize this on http://pythontutor.com/cpp.html#mode=edit
int main()
{
   double* account_pointer = new double;
   *account_pointer = 1000;
   cout << "Allocated one new variable containing " << *account_pointer
      << endl;
   cout << endl;

   int n = 10;
   double* account_array = new double[n];
   for (int i = 0; i < n; i++)
   {
      account_array[i] = 1000 * i;
   }   
   cout << "Allocated an array of size " << n << endl;
   for (int i = 0; i < n; i++)
   {
      cout << i << ": " << account_array[i] << endl;
   }
   cout << endl;

   // Doubling the array capacity 
   double* bigger_array = new double[2 * n];
   for (int i = 0; i < n; i++)
   {
      bigger_array[i] = account_array[i];
   }
   delete[] account_array; // Deleting smaller array
   account_array = bigger_array;
   n = 2 * n;

   cout << "Now there is room for an additional element:" << endl;
   account_array[10] = 10000;
   cout << 10 << ": " << account_array[10] << endl;    
   
   delete account_pointer;
   delete[] account_array; // Deleting larger array
   
   return 0;
}
Posted by: Guest on April-02-2020
0

pointers c++

// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
  int g;
  g = (*functocall)(x,y);
  return (g);
}

int main ()
{
  int m,n;
  int (*minus)(int,int) = subtraction;

  m = operation (7, 5, addition);
  n = operation (20, m, minus);
  cout <<n;
  return 0;
}
Posted by: Guest on June-15-2021

Browse Popular Code Answers by Language