We have defined a Tool class that has private variables accessed through get and set functions which are public.
The setname function is prototyped as :
void setname(const char * val);
in the header file.
in the cpp file:
void Tool::setname(const char * val)
{
strcpy(name, val);
}
i then make an array of the class by saying:
Tool catalog[60];
int toolindex = 0;
however when we pass name to the function like
catalog[toolindex].setname("make this the name\0");
the program segfaults. I wrote a driver file for my tool class which made an object and set the values of each variable in the private data section of the class, then it output the class values to the screen just fine. Worked great then, but now it seems that it doesnt like the fact that I have an array of tools.. im not sure whats going on here.
The first argument to strcpy is the destination string, which you've called "name", but you didn't show how you declared it.
If name is declared as [code=c]char name[50];[/code], then it shouldn't crash.
But if name is declared as [code=c]char *name;[/code], then it is probably an unitialized pointer that points into bogus memory, and will cause a segfault.
Functions like strcpy() etc are highly discouraged as they do no bounds checking. In your case, you've failed to allocate memory to your pointers, thus creating buffer overflows.