swtirs
asked on
Need help in resolving an error in the attached code
When the below code is executed, it gives an error in the statement below statement.
T=Binary_Tree(list, 0, number);
The error says.
"Non portable pointer assignment in function main". I am using TurboC on win32.
#include<stdio.h>
#include<conio.h>
/* -------------------------- ---------- ---------- --------- */
/* Definitions */
struct node
{
char info;
struct node *left_child;
struct node *right_child;
};
struct node *binary_tree(char *, int, int);
void output(struct node*, int);
void pre_order(struct node*);
void in_order(struct node*);
/* -------------------------- ---------- ---------- --------- */
/* Function to create an binary tree */
struct node * binary_tree (char * list, int lower, int upper)
{
struct node * node;
int mid=(lower+upper)/2;
node=(struct node*)malloc(sizeof(struct node));
node->info=list [mid];
if(lower>=upper)
{
node->left_child=NULL;
node->right_child=NULL;
return(node);
}
if(lower<=mid-1)
node->left_child=binary_tr ee(list,lo wer,mid-1) ;
else
node->left_child=NULL;
if(mid+1<=upper)
node->right_child=binary_t ree(list,m id+1,upper );
else
node->right_child=NULL;
return(node);
}
/* -------------------------- ---------- ---------- --------- */
/* output Function */
void output(struct node *T, int level)
{
int i;
if(T)
{
output(T->right_child, level+1);
printf("\n");
for(i=0;i<level;i++)
printf(" ");
printf(" %c", T->info);
output(T->left_child,level +1);
}
}
/* -------------------------- ---------- ---------- --------- */
/* Pre_order Traversal */
void pre_order (struct node *node)
{
if(node)
{
printf("%c", node->info);
pre_order (node->right_child);
pre_order(node->right_chil d);
}
}
/* -------------------------- ---------- ---------- --------- */
/* In_order traversal */
void in_order(struct node *node)
{
if(node)
{
in_order(node->left_child) ;
printf("%c",node->info);
in_order(node->right_child );
}
}
/* -------------------------- ---------- ---------- --------- */
/* The entry point - Main */
void main()
{
char list[100];
int number = 0;
char info;
char choice;
struct node *T=(struct node*)malloc(sizeof(struct node));
T=NULL;
printf("\n Input choice 'b' to break:");
choice=getchar();
fflush(stdin);
while(choice!='b')
{
printf("\n Input information of the node:");
scanf("%c", &info);
list[number++] = info;
fflush(stdin);
printf("\n Input choice 'b' to break:");
choice=getchar();
fflush(stdin);
}
number--;
printf("\n number of elements in the list is %d", number);
T=Binary_Tree(list, 0, number);
output(T,1);
printf("\n pre_order traversal\n');
pre_order(T);
printf("\n In_order traversal \n");
in_order(T);
T=Binary_Tree(list, 0, number);
The error says.
"Non portable pointer assignment in function main". I am using TurboC on win32.
#include<stdio.h>
#include<conio.h>
/* --------------------------
/* Definitions */
struct node
{
char info;
struct node *left_child;
struct node *right_child;
};
struct node *binary_tree(char *, int, int);
void output(struct node*, int);
void pre_order(struct node*);
void in_order(struct node*);
/* --------------------------
/* Function to create an binary tree */
struct node * binary_tree (char * list, int lower, int upper)
{
struct node * node;
int mid=(lower+upper)/2;
node=(struct node*)malloc(sizeof(struct
node->info=list [mid];
if(lower>=upper)
{
node->left_child=NULL;
node->right_child=NULL;
return(node);
}
if(lower<=mid-1)
node->left_child=binary_tr
else
node->left_child=NULL;
if(mid+1<=upper)
node->right_child=binary_t
else
node->right_child=NULL;
return(node);
}
/* --------------------------
/* output Function */
void output(struct node *T, int level)
{
int i;
if(T)
{
output(T->right_child, level+1);
printf("\n");
for(i=0;i<level;i++)
printf(" ");
printf(" %c", T->info);
output(T->left_child,level
}
}
/* --------------------------
/* Pre_order Traversal */
void pre_order (struct node *node)
{
if(node)
{
printf("%c", node->info);
pre_order (node->right_child);
pre_order(node->right_chil
}
}
/* --------------------------
/* In_order traversal */
void in_order(struct node *node)
{
if(node)
{
in_order(node->left_child)
printf("%c",node->info);
in_order(node->right_child
}
}
/* --------------------------
/* The entry point - Main */
void main()
{
char list[100];
int number = 0;
char info;
char choice;
struct node *T=(struct node*)malloc(sizeof(struct
T=NULL;
printf("\n Input choice 'b' to break:");
choice=getchar();
fflush(stdin);
while(choice!='b')
{
printf("\n Input information of the node:");
scanf("%c", &info);
list[number++] = info;
fflush(stdin);
printf("\n Input choice 'b' to break:");
choice=getchar();
fflush(stdin);
}
number--;
printf("\n number of elements in the list is %d", number);
T=Binary_Tree(list, 0, number);
output(T,1);
printf("\n pre_order traversal\n');
pre_order(T);
printf("\n In_order traversal \n");
in_order(T);
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER