Part of the difficulty in answering questions lies in trying to understand just what is being attempted. As we used to say years before "Short Circuit": "Computers don't do what you want them to do; they only do what you tell them to do." Corollary: it is marital suicide to respond to your cyberphobic spouse's complaints of "why did the stupid computer to that!?" with the truth, "Because that's what you told it to do."
I assume that you do intend char *branches;
to be an array of character pointers, such that you will have up to 100 strings all accessed through branches. Furthermore, I assume that you had initialized branches with an initialization array or by initializing it to all NULLs and then adding strings to it new'ing/malloc'ing to each applicable entry and copying a string to it, such that every single entry in branches either contains a NULL or a pointer to a valid and properly terminated string. In other words, I assume that branches has been set up correctly.
BTW, if branches is declared outside the scope of a function (has file scope, therefore is of static storage class -- stored in memory), then most compilers initialize it to all zeros at start-up. DO NOT RELY ON THIS BEHAVIOR!
Always initialize a variable explicitly if you depend on its initial value; it also makes the code more readable.
OTOH, if branches is declared inside of a function, then it is of the auto storage class (stored on the stack) and is not
initialized and is therefore almost guaranteed to contain garbage. Garbage pointer values are sure death for a program.
How I would write it (with some program structure for context):
// first, properly initialize branches and load it with valid strings
// now write it out to disk
outfile = fopen("sample.o", "r+w");
// actually, I would have used a for-loop here, but that's a matter of style
i = 0;
while( branches[ i ] != NULL && i < 100)
// your statement
// fwrite(&branches. sizeof(branches), 1, outfile);
// how I would do it using fwrite
fwrite(branches[ i ], sizeof(char), strlen(branches[ i ])+1, outfile);
You are writing strings out to the disk file. A string is access by a pointer. fwrite is looking for a pointer and branches[ i ] is already a pointer so no address operator is needed.
Besides which, there is some confusion as to just what you are pointing to with &branches. A recent thread here indicates that &branches would be the same as &branches. So if you write &branches out to disk, then you will be writing all the pointers
, not the string data that each one is pointing to. Pointer values have no meaning outside of the program that they are being used in; I can think of no valid reason to write a pointer value out to disk -- outside of logging a pointer error or a heap-usage study, maybe. branches is the pointer to the first string, branches is the pointer to the second string, etc. Therefore, branches[ i ] is the variable you want to pass to fwrite so that you will write out the string data itself.
Normal usage of the size and count arguments of fwrite and similar functions is that size is the size of the data type and count is how many items of this data type. The data type is char whose length is one. I used the sizeof() function to illustrate this point.
strlen() returns the length of the string minus
the null terminator. I assumed that within the output file you would want to have a delimiter between the strings, that you would want to separate them with a special marker. For this, I decided to use the NULL, so I simply extended the count by one to include it. You might want to use a different delimiter.
BTW, by not specifying whether you are opening the file in text mode or binary mode, you are allowing it to default to whatever _fmode is (according to Visual C++6 help). This is text by default (again according to VC++6; actual performance may vary between different compilers). To open a file explicitly in text or binary mode, include in the mode parameter a 't' or a 'b'.
fwrite is normally used for binary files. For a text file, I would have used fprintf:
Here, I separated the strings by putting each one on its own line in the text file. You could also choose to use commas or also use quote marks, etc.
As for what exactly caused core to dump, I couldn't say. Looks like it probably got hold of a bad pointer.