Category : pointers

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] = ..

Read more

#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 ..

Read more

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 ..

Read more

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 ..

Read more

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; ..

Read more