Answers for "pointer variable c++"

C++
2

pointer c++

int myvar = 6;
int pointer = &myvar; // adress of myvar
int value = *pointer; // the value the pointer points to: 6
Posted by: Guest on September-12-2021
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
1

pointers in c++

void simple_pointer_examples() {
    int   a;  // a can contain an integer
	int*  x;  // x can contain the memory address of an integer. 
 	char* y;  // y can contain the memory address of a char. 
 	Foo*  z;  // z can contain the memory address of a Foo object.  
 
    a = 10;
    x = &a;   // '&a' extracts address of a 
  
    std::cout <<  x << std::endl; // memory address of a => 0x7ffe9e25bffc
    std::cout << *x << std::endl; //          value of a => 10
}
Posted by: Guest on July-15-2021
4

pointers in cpp

#include <iostream>
using std::cout;

int main() {
  /* 
  Some things to keep in mind:
  	-you shouldn't circumvent the type system if you are creating raw ptrs
  	and don't need to "type pun" or cast (don't use void ptrs)
    -ptr types only reference memory (which are integers), not actual data, thus
    they should not be treated as data types
    char* is just 1 byte of mem, int* is just 4 bytes of mem, etc
    - '*' means that you are creating a pointer which "points" to the mem address
    of a variable
    - '&', in this case, means "get the mem address of this variable"
  */
  
  void* ptr; // a pointer that doesn't reference a certain size of memory
  int* int_ptr; // a pointer that points to data with
  				// only 4 bytes of memory (on stack)
  
  int a = 5; // allocates 4 bytes of mem and stores "5" there (as a primitive)
  ptr = &a; // can only access the memory address of 'a' (not the data there)
  
  int b = 45; 
  int_ptr = &b; // can access both memory address and data of 'b'
  
  cout << ptr << "n"; // prints mem address of 'a'
  /*cout << *ptr << "n"; <- this will error out; a void ptr cannot be 
  							 derefrenced */
  cout << *(int*)ptr << "n"; // type punning to get around void ptr (extra work)
  
  cout << int_ptr << "n"; // mem address of b
  cout << *int_ptr << "n"; // data stored at b
  
  /* -- OUTPUTS -- */
  /*
  	some memory address (arbitrary) which contains 05 00 00 00 as its data
  	5
    some memory address (arbitrary) which contains 2D 00 00 00 as its data
    45
  */
  
  return 0; // you only need this if "main" isnt the linker entry point
  			// you also don't care
  
  // ur also probably wondering why I didn't using namespace std... cherno
}
Posted by: Guest on July-04-2020
0

c++ pointer

#include <iostream>

void pointers_with_dynamic_memory() {
	int  arr_size;   // arr_size can contain an integer
    int* x;          // x can contain the memory address of an integer.
    
    std::cout << "enter an integer > 0: ";
  	std::cin >> arr_size; 
    
    // value of x is the starting address of a heap-allocated block ints	
    x        = new int[arr_size];  
  
  	x[0]     = 10;  // set first element of array to 10
    *x       = 50;  // set first element of array to 50
    *(x + 1) = 100; // +1 offsets automatically by the value of sizeof(int)
  
    delete [] x;
}
Posted by: Guest on July-10-2021
2

what is this pointer in c++

Every object in C++ has access to its own address through an important pointer called this pointer.
 The this pointer is an implicit parameter to all member functions. 
Therefore, inside a member function,
 this may be used to refer to the invoking object.

Friend functions do not have a this pointer,
 because friends are not members of a class. 
Only member functions have a this pointer.
Posted by: Guest on May-14-2021

Browse Popular Code Answers by Language