answersLogoWhite

0

What is inventory pointer c?

User Avatar

Anonymous

12y ago
Updated: 8/20/2019

// Inventory Pointer

// Demonstrates returning a pointer

#include

<iostream>

#include

<string>

#include

<vector>

using

namespace std;

//returns a pointer to a string element

string

* ptrToElement(vector<string>* const pVec, int i);

int

main()

{

vector<string> inventory;

inventory.push_back(

"sword");

inventory.push_back(

"armor");

inventory.push_back(

"shield");

//displays string object that the returned pointer points to

cout <<

"Sending the objected pointed to by returned pointer:\n";

cout << *(ptrToElement(&inventory, 0)) <<

"\n\n";

//assigns one pointer to another - inexpensive assignment

cout <<

"Assigning the returned pointer to another pointer.\n";

string* pStr = ptrToElement(&inventory, 1);

cout <<

"Sending the object pointed to by new pointer to cout:\n";

cout << *pStr <<

"\n\n";

//copies a string object - expensive assignment

cout <<

"Assigning object pointed by pointer to a string object.\n";

string str = *(ptrToElement(&inventory, 2));

cout <<

"Sending the new string object to cout:\n";

cout << str <<

"\n\n";

//altering the string object through a returned pointer

cout <<

"Altering an object through a returned pointer.\n";

*pStr =

"Healing Potion";

cout <<

"Sending the altered object to cout:\n";

cout << inventory[1] << endl;

return 0;

}

string

* ptrToElement(vector<string>* const pVec, int i)

{

//returns address of the string in position i of vector that pVec points to

return &((*pVec)[i]);

}

User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

How can you offset a pointer in C?

Increment or decrement the pointer by the required offset.


What is pointer to a member in objective c?

It is a pointer that points to a member of a structure.


Pointer arithemetic in C?

no


What is the purpose of pointer in C?

the purpose of pointer in c for saving the memory space.,and reduce the length and complexity of the program


WHAT IS POINTER TO POINTER IN C POINTER?

Pointer in C is Memory Reference. It stores memory address of any variable, constant, function or something you later use in your programming. Pointer basically used to ease the referencing of variables and others or in polymorphism and inheritance.


What is Dazzling Pointer in c plus plus?

The pointer that points to a block of memory that does not exist is called a dazzling pointer or wild pointer


What is stream pointer in c?

C does not have stream pointers.


Define pointer in C?

Pointer is a variable, A variable that stores the address of another variable. Size of a pointer is 2 bytes.


How do you declare a pointer variable in c?

int* pint; // instantiate a pointer to an int. float* pflt; // instantiate a pointer to a float.


Define pointer to pointer in c?

Double (**) is used to denote the double pointer. As we know the pointer stores the address of variable, Double pointer stores the address of any pointer variable. Declaration : int **ptr2Ptr;


Pointer to pointer in c?

Usable. A prominent example is param argv of function main.


What pointer type will you use to implement a heterogeneous linked list in c?

void pointer