I am learning about l-values and r-values. I understand an l-value to be: an object that occupies a location in memory and it is addressable. Why is a pointer expression not considered a l-value? With the code below: int main(){ int x { 100 }; int* ptr = &x; int& l_val_ref = ptr; //problem on ..
Category : pointers
I am creating a binary tree in C++, and I have some problems when using smart pointers inside of the node class. When using normal pointers, everything works fine, but with smart pointers it is just not working. I think that the problem is in this line in the insert method: ”’ binaryNode* node = ..
My string-dynamic-array.cpp file #include <iostream> #include <string> class DynamicArray { public: DynamicArray() : mCapacity(1), mNumberOfElements(0) { mArray = new std::string[mCapacity]; } DynamicArray(int size) : mCapacity(size), mNumberOfElements(0) { mArray = new std::string[mCapacity]; } DynamicArray(const DynamicArray& array) : mCapacity(array.getCapacity()), mNumberOfElements(array.length()) { mArray = new std::string[mCapacity]; for (size_t i = 0; i < mCapacity; ++i) { mArray[i] = ..
#include <iostream> int main() { int arr[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}}; int* ptr[4] = arr; } Why does the above code produce the array must be initialized with a brace-enclosed initializer error? I’ve seen other posts with this issue but the solution was to add in curly brackets, which I already have. Source: Windows Questions ..
I am trying to access the elements of vector by using a pointer to it. I found that you can do something similar to the following: std::vector<char> vector= { ‘a’, ‘b’,’c’,’d’ }; std::vector<char>* vecPointer = &vector; std::vector<char>& vecReference = *vecPointer; char value = vecReference[2]; However, I am concerned about the cost. Is this any better ..
I have the following little program to get a better understanding of C++ pointers. I have a question to 1 of the lines. So here is the .h file: #pragma once int Value; int CalculateWithValue(int inbound); int CalculateWithAddress(int& inbound); The .cpp file: #include <iostream> #include "PassBy.h" int main() { Value = 3; int *p = ..
Im currently struggling to implement a insertion sort algorithm for a 2D pointer array. NO Qsort solutions please, it needs to be old school Here is my code void insertionSort(double **arr, int rows,int c) { double *temp; int i, j; for (i = 1; i < rows; i++) { temp = *(arr+i); j = i ..
int main() { char Grid[8][8] = { ‘~’,’~’,’~’,’~’,’~’,’~’,’~’,’~’, ‘~’,’~’,’~’,’~’,’~’,’~’,’~’,’~’, ‘~’,’~’,’~’,’~’,’~’,’~’,’~’,’~’, ‘~’,’~’,’~’,’~’,’~’,’~’,’~’,’~’, ‘~’,’~’,’~’,’~’,’~’,’~’,’~’,’~’, ‘~’,’~’,’~’,’~’,’~’,’~’,’~’,’~’, ‘~’,’~’,’~’,’~’,’~’,’~’,’~’,’~’, ‘~’,’~’,’~’,’~’,’~’,’~’,’~’,’~’, }; int ShipCount = 1; while ( ShipCount < 6){ ShipPlacement(ShipCount, Grid[8][8]); ShipCount++; } ShowGrid(Grid[8][8]); return 0; } int ShipPlacement(int Shipcount, char Grid[8][8]) { srand(time(NULL)); int VerticalHorizontal = rand() % 2; //Randomly generates a number that will decide if the ..
this is my sample code which is similar to my project, to simplify I created this dummy program. #include <iostream> using namespace std; struct str1{ int a1; }; void fun(str1 *x); int main() { str1 s; fun(&s); cout<<"print "<<s.a1<<endl; return 0; } void fun(str1 *x){ struct test{int c1;}tst; tst.c1 =7; for(int i= 0; i<9;i++){ x->a1=tst.c1; ..
int arr[] = { 1, 2, 3, 4 }; printf("%d %d", sizeof(arr), sizeof(arr[0])); //16 4 Since array is a pointer on it’s first element, I expected it to have the same size as it’s first element has. However, it counts 4 elems. Why does it happen like that? Source: Windows Que..
Recent Comments