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;
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 */

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) !

Cheers,

NiNTENDU.

2
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by NiNTENDU
char foo[1];

Change that to

char foo[2];

I dunno what happenes there, but that works.

And always typedef the struct after it has bas been defined. And intialise the tree pointer at main to NULL before you send that the function add_name. That is must. You have been lucky in this case, otherwise the you would have ended wiitht some SF with the broken code.

struct _node
{
char *name;
double val;
struct _node *left;
struct _node *right;
};

typedef struct _node *tree;

IN MAIN

tree *Tree
Tree = NULL;

ssharish

char foo[1];

Change that to

char foo[2];

I dunno what happenes there, but that works.

And always typedef the struct after it has bas been defined. And intialise the tree pointer at main to NULL before you send that the function add_name. That is must. You have been lucky in this case, otherwise the you would have ended wiitht some SF with the broken code.

struct _node
{
char *name;
double val;
struct _node *left;
struct _node *right;
};

typedef struct _node *tree;

IN MAIN

tree *Tree
Tree = NULL;

ssharish

Thanks for the reply but none of yours hints solve my problem:

I initialize to null the tree var but I don't see your point of typedef after initialization altrought is perfecly legal to typedef before initialization (maybe I'm wrong but all the book I have ever use this technique, even the Deitel & Deitel); whats the difference?

Also I've set char foo[2] but thats ghange anything because now I see three strange character.

Greetings,

NiNTENDU.

int main()
{
tree Tree;
char foo[2];

foo[0] = 'b';
foo[1] = '\0';
Tree = NULL;

print_tree( Tree );

getchar();
return 0;
}

Thats how my main is constructed and my output:

b = 0.000

ssharish

Stupid me... yeah with your method it works... but try to change the lenght ro 3!
I cannot accept to use this kind of dirty tricks :(
However thi is my new code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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 );
}
}

int main ( int argc, char *argv[] )
{
tree Tree = NULL;
char foo[2] = "ab" ;
/* char *foo = "ab"; */ /* This work instead */
print_tree ( Tree );
return 0;
}

It's continue to be weird... :(

Greeting,

Edit: Now I understand that I've to reserve space for the \0 char so set it to 3.
but if I'm want to store some random name take from stdin how can I do?
NiNTENDU.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.