Sorry for bother you with this dumb questions... I understand very well math but I'm a noob in C... Hope to "evolve" :(
Anyway I manipulate a simple binary tree this way:
typedef struct _node *tree;
struct _node
{
char *name;
double val;
tree left;
tree right;
};
void add_name ( tree *p_T, char *name )
{
if ( *p_T == NULL ) {
*p_T = ( tree ) malloc ( sizeof ( struct _node ) );
if ( *p_T != NULL )
{
(*p_T)->val = 0;
(*p_T)->name = name;
(*p_T)->left = NULL;
(*p_T)->right = NULL;
}
else
{
puts ( "\nNo memory available. \n" );
}
}else{
strcmp ( name, ( *p_T )->name ) < 0 ?
add_name ( & ( ( *p_T )->left ), name ) :
add_name ( & ( ( *p_T )->right ), name );
}
}
void print_tree ( tree p_T )
{
if ( p_T != NULL )
{
print_tree ( p_T->left );
printf ( "%s = %.3f\n",
p_T->name,
p_T->val );
print_tree ( p_T->right );
}
}
Nothing special... and I use it this way:
int main()
{
tree Tree;
add_name( &Tree, "FooBar" );
print_tree( &Tree );
return 0;
}
And the output is:
FooBar = 0.000
Works like a charm but if I use this:
int main()
{
tree Tree;
char foo[1];
foo[0] = 'b';
/* foo[1] = '\0' */ /* I've also tried this but without success */
add_name( &Tree, foo );
print_tree( &Tree );
return 0;
}
The output is:
A@ = 0.000
Notice that "A@" are always the same (dunnot change every run but in your machine may be different) and there are 2 char instead 1 (the second I think was a null terminator that is missed or am I wrong?).
Why? I need to do this because I need to modify "foo".
This drive me crazy :( It's really strange (at least for me) !
Help me please :).
Cheers,
NiNTENDU.