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.

Thanks for replying. I wanted to make sure I was understanding that last part right. I also need to parse a command line input and store the items into the struct, I think this is the part where I'm doing something wrong. Updated code:

That's uninitialized, so if you don't fill in MAX_EMOTION_SIZE elements of that array, then this will be freeing random values:

for(i=0; i < (ex_1.num); i++)
free(ex_1.emotion_options[i]);

The solution is to memset() the memory from the original call to zero, or easier yet, use calloc() instead of malloc(). (calloc() is meant to be used for arrays anyway, and this is an array.)

Your process_input() function could use sscanf() if you wanted -- but the way you have it is fine too (and probably what I would have done anyway).

(ex_1->num)++;

Those parentheses aren't required, but they're fine, of course. What isn't fine is that you don't check if you're incrementing ex_1->name above or equal to MAX_EMOTION_SIZE, so you have a possible buffer overrun here.

Declaring variables in the middle of a block like that is C99. If you want C89-compatible code (which I suspect), put the variable declaration at the beginning of the function -- or perhaps refactor the freeing code into a separate function.

If you don't mind C99-specific code, you might as well declare the variable in the for loop.

for(int i=0; i < (ex_1.num); i++)

Casting malloc() isn't always a good idea, because it can hide changes to the type of the variable and so on.

Thanks for the comments dwks. Yes I do desire c89 (after I just figured out what that meant). I made all the fixes. I have a question about the freeing random values and was wondering why that needed to be corrected.

Does that mean that a pointer upon creation randomly points to some address initially and potentially toward memory used by another program? Calloc/memset address this issue by setting the pointer values to null?

If so, then why is the default for the pointer to point to some random address and not null upon declaration?

Thanks for the comments dwks. Yes I do desire c89 (after I just figured out what that meant).

Yeah, most of the time it's a good idea. Sorry I didn't explain it further.

Does that mean that a pointer upon creation randomly points to some address initially and potentially toward memory used by another program?

Yes. Accessing such memory could lead to data corruption or at the very least segmentation faults.

Calloc/memset address this issue by setting the pointer values to null?

Correct.

If so, then why is the default for the pointer to point to some random address and not null upon declaration?

Why? Because initializing things to zero takes time. C is an efficient language. It does as little as possible. C figures that if you need the memory initialized to zero, you will use calloc or memset. :)

[edit] About casting malloc():

In the old days of K&C C (before C89!), there was no such thing as void *. So malloc() had to return char * instead. Which meant that you had to cast the return value of malloc() to whatever you were using, say, int *.

Now, however, void * does exist, and that is what malloc() returns. C (even C89) says that you can assign a void pointer to any other pointer without a cast. Hence, casting malloc() is no longer required. And in fact, it's possible to make an error in a cast, so you might as well leave out the cast.

Casting malloc() can also hide the fact that stdlib.h is not included. Without a cast, the compiler will use the implicit int rule and assume that malloc() returns int. Without a cast, an assignment from int to, say, int * will cause an error or warning; casting the value hides this warning. You can get some really nasty errors this way. (malloc(10) is passing an int for the parameter, but malloc() takes a size_t, which may not be the same size as an int . . . .)

This issue is compounded by the fact that in C++, you do need a cast. But in C++, you're better off using new and delete anyway.

You're best off not casting malloc() at all. :) [/edit]

[edit=2] Another thing:

Hard-coding types like this

int *p = malloc(10 * sizeof(int));

is doable, but it makes more sense to use the variable itself:

int *p = malloc(10 * sizeof(*p));

That way the code still works when you change the type of p. sizeof is a compile-time value in this case, so don't worry, it's just as efficient. [/edit]

Hi. so this is actually a continuation from another question of mine[Here](https://www.daniweb.com/programming/software-development/threads/506795/dynamically-add-values-into-datagridview-cell-from-listbox-vb2010) but i was advised to start a new thread as the original question …

I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...

Write a C program that should create a 10 element array of random integers (0 to 9). The program should total all of the numbers in the odd positions of the array and compare them with the total of the numbers in the even positions of the array and indicate ...