problem in passing a value into a structure

I

iskeletor

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define STUDENT_NUMBER 68
#define ARRAY_LENGTH 10

struct node{
char Name,Surname;
int data,no;
struct node *left,*right,*datA;
};

typedef struct node node;

/*prototypes*/
void buildTree();
node* createNode(char[],char[],int);
//void insert(node,int,char,char);

node *root=NULL,*nod=NULL;
long ID,order,nodeNumber,depth=0;
char name[10],familyName[10];

int main()
{
buildTree();

return 0;
}


void buildTree(){
FILE *file;


if((file=fopen("data.txt","r"))==NULL){
printf("File couldn't be opened!\n");

}else{



while(!feof(file)){

fscanf(file,"%ld%ld%s%s",&order,&ID,name,familyName);

printf("order=%ld,ID=%ld,name=%s,familyName=%s\n",order,ID,name,familyName);


root=createNode(*familyName,*name,ID);/*HERE THERE IS A
PROBLEM:COMPILER WANTS CASTING BUT HOW WILL I DO IT?*/


}
}

fclose(file);
}




node* createNode(char srnm[],char nm[],int noo){
node* newNode;
newNode=(node*)(malloc(sizeof(node)));
newNode->Surname=*(srnm);
newNode->Name=*(nm);
newNode->no=noo;
newNode->left=NULL;
newNode->right=NULL;
return newNode;
}

void add(node *nd,long id[],char namE[],char surnamE[]){

while(pow(2,depth+1)!=STUDENT_NUMBER){
depth++;
}

if(depth%3==0){
*nod->datA=namE;/*HERE SAME PROBLEM AGAIN*/

}

if(depth%3==2){
*nod->datA=id; /*HERE SAME PROBLEM AGAIN*/

}

if(depth%3==0){
*nod->datA=surnamE; /*HERE SAME PROBLEM AGAIN*/

}

if(nod.datA>nd->no){
if(nd->right==NULL){
nd->right=createNode(namE,surnamE,id);
}else{
add(nd->right,id,namE,surnamE);
}
}
else
{
if(nd->left==NULL){
nd->left=createNode(namE,surnamE,id);
}else{
add(nd->left,id,namE,surnamE);
}
}


}
 
N

Nick Keighley

iskeletor said:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define STUDENT_NUMBER 68
#define ARRAY_LENGTH 10

struct node{
char Name,Surname;
int data,no;
struct node *left,*right,*datA;
};

typedef struct node node;

/*prototypes*/
void buildTree();
node* createNode(char[],char[],int);
//void insert(node,int,char,char);

node *root=NULL,*nod=NULL;
long ID,order,nodeNumber,depth=0;
char name[10],familyName[10];

int main()
{
buildTree();

return 0;
}


void buildTree(){
FILE *file;


if((file=fopen("data.txt","r"))==NULL){
printf("File couldn't be opened!\n");

}else{



while(!feof(file)){

fscanf(file,"%ld%ld%s%s",&order,&ID,name,familyName);

printf("order=%ld,ID=%ld,name=%s,familyName=%s\n",order,ID,name,familyName);


root=createNode(*familyName,*name,ID);/*HERE THERE IS A
PROBLEM:COMPILER WANTS CASTING BUT HOW WILL I DO IT?*/


root = createNode (familyName, name, ID);


}
}

fclose(file);
}

void add(node *nd,long id[],char namE[],char surnamE[]){

while(pow(2,depth+1)!=STUDENT_NUMBER){
depth++;
}

if(depth%3==0){
*nod->datA=namE;/*HERE SAME PROBLEM AGAIN*/

no it isn't, read your compiler diagnostics. This is a mess. *nod is
struct node *not* a struct node*. Get rid of the *. namE is char* but
datA is a struct node. struct node has no char* fields. Go back and
re-read the ptr section of your textbook. Then redesign your program.

Fix the layout (is your space key broken?). Don't have similar and
confusing names (nod, node), don't mix case in identifiers (namE).

<snip>

my compiler was *very* noisy. This is the output for a (slightly)
cleaned up version of your code.

G:\tmp\iskel.c(59) : error C2018: unknown character '0xad'
G:\tmp\iskel.c(62) : warning C4047: 'function' : 'char *' differs in
levels of indirection from 'char '
G:\tmp\iskel.c(62) : warning C4024: 'createNode' : different types for
formal and actual parameter 1
G:\tmp\iskel.c(62) : warning C4047: 'function' : 'char *' differs in
levels of indirection from 'char '
G:\tmp\iskel.c(62) : warning C4024: 'createNode' : different types for
formal and actual parameter 2
G:\tmp\iskel.c(101) : warning C4047: 'function' : 'char *' differs in
levels of indirection from 'char '
G:\tmp\iskel.c(101) : warning C4024: 'strcpy' : different types for
formal and actual parameter 1

this one is my fault...

G:\tmp\iskel.c(108) : error C2115: '=' : incompatible types
G:\tmp\iskel.c(115) : error C2115: '=' : incompatible types
G:\tmp\iskel.c(121) : error C2231: '.datA' : left operand points to
'struct', use '->'
G:\tmp\iskel.c(121) : warning C4047: '>' : 'struct node *' differs in
levels of indirection from 'int '
G:\tmp\iskel.c(123) : warning C4047: 'function' : 'int ' differs in
levels of indirection from 'long *'
G:\tmp\iskel.c(123) : warning C4024: 'createNode' : different types for
formal and actual parameter 3
G:\tmp\iskel.c(131) : warning C4047: 'function' : 'int ' differs in
levels of indirection from 'long *'
G:\tmp\iskel.c(131) : warning C4024: 'createNode' : different types for
formal and actual parameter 3
 
B

Barry Schwarz

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define STUDENT_NUMBER 68
#define ARRAY_LENGTH 10

struct node{
char Name,Surname;
int data,no;
struct node *left,*right,*datA;
};

typedef struct node node;

/*prototypes*/
void buildTree();
node* createNode(char[],char[],int);
//void insert(node,int,char,char);

node *root=NULL,*nod=NULL;
long ID,order,nodeNumber,depth=0;
char name[10],familyName[10];

int main()
{
buildTree();

return 0;
}


void buildTree(){

Since the function returns nothing, how is anyone to know if it
succeeded or not.
FILE *file;


if((file=fopen("data.txt","r"))==NULL){
printf("File couldn't be opened!\n");

}else{

Why are you wasting all this vertical space. Most of us like to be
able to see a reasonable amount of code in one view without scrolling.
while(!feof(file)){

This will cause you to process the last set of data twice. feof
returns 1 only **after** you try to read past the end of file.
fscanf(file,"%ld%ld%s%s",&order,&ID,name,familyName);

printf("order=%ld,ID=%ld,name=%s,familyName=%s\n",order,ID,name,familyName);


root=createNode(*familyName,*name,ID);/*HERE THERE IS A
PROBLEM:COMPILER WANTS CASTING BUT HOW WILL I DO IT?*/

No the compiler does not want casting. The compiler wants you to pass
arguments that match the prototype. What is the type of the first
parameter in the createNode prototype? What is the type of
*familyName? Do you see the difference?

How many arguments are you supposed to pass to createNode? How many
are in the above statement?

createNode allocates memory. It returns the address of the allocated
memory. You assign this address to root. On the next iteration, you
replace the existing value of root with the new value returned by
createNode, thereby losing any chance of using the first allocated
area. This is called a memory leak.
}
}

fclose(file);
}




node* createNode(char srnm[],char nm[],int noo){
node* newNode;
newNode=(node*)(malloc(sizeof(node)));

Don't cast the return from malloc.
newNode->Surname=*(srnm);

Surname is a char. *srnm (with or without the parentheses) is also a
char so this is a legal assignment. What do you expect to do with
only one character from the name?
newNode->Name=*(nm);
newNode->no=noo;
newNode->left=NULL;
newNode->right=NULL;
return newNode;
}

void add(node *nd,long id[],char namE[],char surnamE[]){

You never call this function.
while(pow(2,depth+1)!=STUDENT_NUMBER){

STUDENT_NUMBER is not a power of 3. This while never ends.
depth++;
}

if(depth%3==0){
*nod->datA=namE;/*HERE SAME PROBLEM AGAIN*/

What same problem? Did your compiler really ask for a cast here?

nod is a pointer to struct. *nod is the struct it points to. To
access a member of a struct, use the . operator, not the -> one. If
you want to use the -. operator, the left operand must be a pointer to
struct. Drop the *.

datA is a pointer to struct. name is a pointer to char. The two are
incompatible. What are you really trying to accomplish?
}

if(depth%3==2){
*nod->datA=id; /*HERE SAME PROBLEM AGAIN*/

id is an array of long. In this context, it evaluates to the address
of id[0] with type pointer to long. This pointer is incompatible with
datA which is a pointer to struct. Did you perhaps want datA to be a
void* so it could point to any kind of data you want?
}

if(depth%3==0){
*nod->datA=surnamE; /*HERE SAME PROBLEM AGAIN*/

Consistency is a virtue. datA hasn't changed. surnameE has the same
type as namE. This should produce the same error.
}

if(nod.datA>nd->no){

datA is a pointer. no is an int. They are incompatible. What are
you trying to do?

You might want to use a little horizontal white space in an expression
like this.
if(nd->right==NULL){
nd->right=createNode(namE,surnamE,id);
}else{
add(nd->right,id,namE,surnamE);
}
}
else
{
if(nd->left==NULL){
nd->left=createNode(namE,surnamE,id);
}else{
add(nd->left,id,namE,surnamE);
}
}


}


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top