here is example how Pointers are used to store and manage the addresses of dynamically allocated blocks of memory
#include <iostream>
#include <stdio.h>
using namespace std;
struct Item{
int id;
char* name;
float cost;
};
struct Item*make_item(const char *name){
struct Item *item;
item=malloc(sizeof(struct Item));
if (item==NULL)
return NULL;
memset(item,0,sizeof(struct Item));
item->id=-1;
item->name=NULL;
item->cost=0.0;
/* Save a copy of the name in the new Item */
item->name=malloc(strlen(name)+1);
if (item->name=NULL){
free(item);
return NULL;
}
strcpy(item->name,name);
return item;
}
int main(){
return 0;
}
but here is mistakes
1
>------ Build started: Project: dynamic_memory, Configuration: Debug Win32 ------
1> dynamic_memory.cpp
1>c:\users\david\documents\visual studio 2010\projects\dynamic_memory\dynamic_memory.cpp(11): error C2440: '=' : cannot convert from 'void *' to 'Item *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>c:\users\david\documents\visual studio 2010\projects\dynamic_memory\dynamic_memory.cpp(20): error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
what is wrong?please help