I made code of bob the builder, it's a structure, every bob the builder has name and two more integers (doesn't really matter). there are three functions
initialize the struct(with "bob" and 0 and 3)
the second function get two structures and it's needs to copy between those structures
the third function is to free the names(char*) of every bob.
Firstly, the second function(copy) went wrong in the debugging because it didn't copy the name(Need your help in analyzing why it happened) and secondly, the code crashed in the free function. Can someone tell me how to free names(char*) of structures?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define LENGTH_OF_BOB 4
typedef struct bobTheBuilder
{
char* name;
int fixed;
int maxFix;
}bob;
//typedef struct bobTHeBuilder bob;
void deleteBob(bob currBob);
void initBob(bob *currBob);
void copyStruct(bob* dst, bob src);
int main(void)
{
bob currBob = {0,0,0};
bob secondBob;
initBob(&currBob);
copyStruct(&secondBob, currBob);
deleteBob(currBob);
deleteBob(secondBob);
system("PAUSE");
return 0;
}
/*
*/
void initBob(bob *currBob)
{
char* str = (char*)calloc(LENGTH_OF_BOB, sizeof(char));
char string[] = "bob";
if (str)
{
strcat(string, "\0");
str = string;
currBob->name = str;
currBob->fixed = 0;
currBob->maxFix = 3;
}
}
/*
*/
void deleteBob(bob currBob)
{
free(currBob.name);
}
void copyStruct(bob* dest, bob src)
{
dest->fixed = src.fixed;
dest->maxFix = src.maxFix;
dest->name = (char*)malloc(sizeof(char) *LENGTH_OF_BOB);
strncpy(dest->name, src.name, LENGTH_OF_BOB);
}