c++ - dynamic memory allocation -
Here is how Pointer is used to store and manage the addresses of dynamically allocated blocks of memory
#include & lt; Iostream & gt; # Include & lt; Stdio.h & gt; using namespace std; Struct item {int id; Four names; Boat cost; }; Struct item * make_item (const char * name) {struct item * item; Item = maulock (structure item); If (item == faucet) return tap; MEMSAT (item, 0, size (strip item)); Items, & gt; Id = -1; Items, & gt; Name = zero; Items, & gt; Cost = 0.0; / * Save a copy of the name in new items * / item- & gt; Name = malloc (strlen (name) +1); If (item-> Names = NULL) {free (item); Return tap; } Strcpy (item-> name, name); Object back; } Int main () {Return 0; }
But here are the mistakes
1
& gt; ------ Construction started: Project: dynamic_memory, Configuration: Debug Win32 ------ 1 & gt; Dynamic_memory.cpp 1 & gt; C: \ users \ david \ documents \ visual studio 2010 \ projects \ dynamic_memory \ dynamic_memory.cpp (11): Error C2440: '=': 'Zero *' to 'Items *' 1 & gt; To convert from 'zero *' to a clear artist 1 in non '' wide '1 & gt; C: \ users \ david \ documents \ Visual Studio 2010 \ projects \ dynamic_memory \ dynamic_memory.cpp (20): Requires error C2440: '=': 'Zero *' to 'four *' 1 & gt; A clear cast for conversion from 'zero *' to 'non-wide' is required ========== Build: 0 succeeded, 1 failed, 0 up-to-date , 0 skip ===== =====
What's wrong? Please
line 11 items = molok (size (strip item));
items = (item *) should be maulok (strip item);
line 20 item-> Name = malloc (strlen (name) + 1);
object-> - name = (four *) must be Malloc (strlen (name) + 1);
at line 21 (item-> name = NULL) {
if (item-> name == faucet) {
.
Comments
Post a Comment