473,396 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem in passing a value into a structure

#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,familyNam e);

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

Dec 14 '06 #1
3 2611
iskeletor wrote:
#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,familyNam e);

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);
}
<snip>
>
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
--
Nick Keighley

why isn't there an obfuscated C++ contest?

Dec 14 '06 #2

thank you very much =)

Dec 14 '06 #3
On 14 Dec 2006 00:57:33 -0800, "iskeletor" <zi********@gmail.com>
wrote:
>#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,familyNam e);

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
Dec 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
1
by: Craig | last post by:
I have added a 'Textboxes (A)' to my UI installer project along with a custom action to pass the value back to a class I've written to override the void Install function. As long as the text is...
15
by: John Alway | last post by:
Hello, I'm using a DLL I wrote in C++, and am attempting to call and use it from VB. This works fine for functions where I pass parameters by value, but I can't get pointers to work. I get...
17
by: Christopher Benson-Manica | last post by:
Does the following program exhibit undefined behavior? Specifically, does passing a struct by value cause undefined behavior if that struct has as a member a pointer that has been passed to...
11
by: Macca | last post by:
Hi, I'm writing an application that will pass a large amount of data between classes/functions. In C++ it was more efficient to send a pointer to the object, e.g structure rather than passing...
1
by: dcs | last post by:
Hi, Can someone please help. I have a class that contains the following Structure (called MyStructure) and sub (called MainSub). And this 1st class inherits a 2nd class that contains a sub called...
26
by: the.tarquin | last post by:
Okay, this one has me totally baffled. I have a function, getParsedKey(char* key, char* returnString). I pass in the key I want, it retrieves it from a data structure and puts the value in...
4
by: Michael | last post by:
after update dataTable with this codes, DataRow drNewRow = ff.m_dtWords.NewRow(); drNewRow=str; drNewRow=frequency; ff.m_dtWords.Rows.Add(drNewRow);...
3
by: ishwarbg | last post by:
Hi Everyone, I have a .Net Application, through which I am invoking a function from a legacy DLL developed in C++. My structure in C# contains some data of type double which I need to pass to to...
7
by: pereges | last post by:
which one do you think is better ? I need to make my program efficient and in some places I have passed the copy of a variable which makes life some what easy while writing huge expressions but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.