Hello,
I am having problem with strings within structs and was wondering if someone could help.
I have a struct, i.e.
#include <stdio.h>
#include <strings.h>
struct feeling
{
char *emotion;
char *emotion_options[20];
int num;
};
which i then pass to a function via reference, i.e.
int main()
{
struct feeling ex_1 {"happy",{"yes","no","maybe"},-1};
function(&ex_1);
return 0;
}
and in the function I use string functions on its members and they aren't being preserved/accessed correctly?
void function(struct emotion *test)
{
int size;
printf("%s", test->emotion); /*prints out happy */
size = strlen(test->emotion);
printf("%s",test->emotion); /* prints out random characters */
}
I was wondering why the 'emotion' member would be modified after the strlen call, and what can be done to prevent it? Or really just how I should go about doing this. Tried different string function calls like strcat, strcmp, and have same problem.