GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Jan-2005, 19:15
stacy12 stacy12 is offline
New Member
 
Join Date: Jan 2005
Posts: 9
stacy12 is on a distinguished road

Mrs


can someone help me fix this this program:


everytime i am running the program is giving me this error message:
two or more data types in declaration of `printArray'


here the program code

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20

struct data {
   int value;
   struct data *next;
   struct data *prev;
}

//*****************************************************************************

void printArray(struct data  *iarray){

  if(iarray==NULL) {
  puts("list empty");
  return;
 }

 while(iarray!=NULL){
 printf("value : %-8d\t",iarray->value);
 iarray = iarray->next;
 return;
 }
}//end of function

/*void makeList(struct data *headptr,struct data*tailptr) {
int x,iarray[SIZE]={20,2,10,15,90,4,8,32,59,18,33,51,9,103,83,91,25,39,49,38};


newnodeptr= (struct data *)malloc(sizeof(struct data));
  if(newnodeptr==NULL) {
    printf("error");
    exit(0);
  }
newnodeptr->next=NULL;
newnodeptr->value=iarray[0];

headptr=newnodeptr;
  for(x=1;x<SIZE;x++) {
  //insert = 0;

newnodeptr= (struct data *)malloc(sizeof(struct data));
  if(newnodeptr==NULL) {
    printf("error");
    exit(0);
  }

} */

void addtofront(struct data **headptr) {
struct data *newnodeptr;
int iarray[20]={20,2,10,15,90,4,8,32,59,18,33,51,9,103,83,91,25,39,49,38};
printf("n\Enter a number to the front");
scanf("%d",&iarray[20]);

 
newnodeptr =(struct data *)malloc(sizeof(struct data));
newnodeptr->value=iarray[0];
newnodeptr->next=NULL;

if(*headptr==NULL){ /*create empty list*/
*headptr=newnodeptr;

return;
}
/*add to the head of the list*/
newnodeptr->next = *headptr;
*headptr=newnodeptr;

puts("addition was performed");
}


void addtoEnd(struct data **headptr){
struct data *newnodeptr, *tempnodeptr;
int iarray[20]={20,2,10,15,90,4,8,32,59,18,33,51,9,103,83,91,25,39,49,38};
//int num
printf("n\Enter a number to the end");
scanf("%d",&iarray[20]);

 
newnodeptr =(struct data *)malloc(sizeof(struct data));
newnodeptr->value=iarray[0];
newnodeptr->next=NULL;

//if(*headptr == NULL){ /*create empty list*/
//*headptr=newnodeptr;

puts("addition was performed");
return;

//}
//*headptr = tempnodeptr;
/*bring node to the end*/
while(tempnodeptr->next) { //keep looping until last reached
tempnodeptr=tempnodeptr->next;
}
/*add to existing list*/
tempnodeptr->next=newnodeptr;
puts("additional performed\n");

return;
}



//void addtoCenter(){
//}


void printFtoR(struct data *headptr) {
struct data *currnodeptr;

headptr=currnodeptr;

 while(currnodeptr!=NULL) {
   printf("value : %-8d\t",currnodeptr->value);
   currnodeptr = currnodeptr->next;
   return;
 }


}

void printRtoF(struct data *tailptr) {
struct data *currnodeptr;

tailptr=currnodeptr;

 while(currnodeptr!=NULL) {
   printf("value : %-8d\t",currnodeptr->value);
   currnodeptr = currnodeptr->prev;
   return;
 }

}




void deleteEven(struct data **headptr,struct data **tailptr) {
struct data *currnodeptr,*tempnodeptr,*prevnodeptr;

currnodeptr=*headptr;

while(currnodeptr!=NULL) {
  if(currnodeptr->value%2==0) {
    if(currnodeptr==*headptr) {//front of list
      tempnodeptr=currnodeptr;
      *headptr=currnodeptr->next;
      currnodeptr=currnodeptr->next;
    }
    else {
      tempnodeptr=currnodeptr;
      prevnodeptr->next=currnodeptr->next;
      currnodeptr=currnodeptr->next;
    }
    free(tempnodeptr);
  }
  else {
  prevnodeptr=currnodeptr;
  currnodeptr=currnodeptr->next;
  }
}//end while
}//end function




//******************************************************************************


int main(void)
{

   struct data *headptr = NULL,*tailptr = NULL;
   int iarray[SIZE]={20,2,10,15,90,4,8,32,59,18,33,51,9,103,83,91,25,39,49,38};//array data

   printf("array contents\n");
   printArray(iarray);
   //makeList(&headptr,&tailptr,iarray);
   printf("\nordered list-front to rear\n");
   printFtoR(headptr);
   printf("\nordered list-rear to front\n");
   printRtoF(tailptr);
   deleteEven(&headptr,&tailptr);
   printf("\nordered list-front to rear with even values deleted\n");
   printFtoR(headptr);
   printf("\nordered list-rear to front with even values deleted\n");
   printRtoF(tailptr);



      //system("PAUSE");
      //return 0;

}
Last edited by LuciWiz : 31-Jan-2005 at 15:28. Reason: Please insert your C code between [c] & [/c] tags
  #2  
Old 30-Jan-2005, 20:57
Max Payne's Avatar
Max Payne Max Payne is offline
Regular Member
 
Join Date: Apr 2004
Location: 3° 08 North 101° 42 East
Posts: 332
Max Payne is a jewel in the roughMax Payne is a jewel in the roughMax Payne is a jewel in the rough
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20

struct data 
{
   int value;
   struct data *next;
   struct data *prev;
};

void printArray(data  *iarray)
{

  if(iarray==NULL) {
  puts("list empty");
  return;
 }

 while(iarray!=NULL){
 printf("value : %-8d\t",iarray->value);
 iarray = iarray->next;
 return;
 }
}//end of function

int main(void)
{

   data *headptr = NULL,*tailptr = NULL;
   int iarray[SIZE]={20,2,10,15,90,4,8,32,59,18,33,51,9,103,83,91,25,39,49,38};//array data

   printf("array contents\n");
   printArray(iarray);
   //makeList(&headptr,&tailptr,iarray);
   printf("\nordered list-front to rear\n");
   printFtoR(headptr);
   printf("\nordered list-rear to front\n");
   printRtoF(tailptr);
   deleteEven(&headptr,&tailptr);
   printf("\nordered list-front to rear with even values deleted\n");
   printFtoR(headptr);
   printf("\nordered list-rear to front with even values deleted\n");
   printRtoF(tailptr);



      //system("PAUSE");
      //return 0;

}
__________________
When you say "I wrote a program that crashed Windows," people just stare at you blankly and say "Hey, I got those with the system, for free." Linus Torvalds
  #3  
Old 31-Jan-2005, 11:38
stacy12 stacy12 is offline
New Member
 
Join Date: Jan 2005
Posts: 9
stacy12 is on a distinguished road

Can some on correct my code


i am using data structure in C. i have to use the struct data *headptr. the one u sent me doesn't work either
  #4  
Old 31-Jan-2005, 14:23
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by stacy12
everytime i am running the program is giving me this error message:
two or more data types in declaration of `printArray'


Here's a hint: If you want someone to help you understand the errors, you should list the exact error (paste from your compiler message window to the post). That lets us see what you are seeing. Different compilers have different ways of reporting errors. You could also tell us what compiler and what operating system you are using (sometimes it makes a difference).

Now, that being said, if you got a message like this, for example
Quote:
Error E2176 z.c 14: Too many types in declaration

That tells us (and you) that the compiler is having problems when it gets to line number 14 of your source file (in my case, the file is named z.c).

Now line 14 of my file is:

CPP / C++ / C Code:
void printArray(struct data  *iarray){

And there is nothing wrong with line 14(!)

So what now? Well, look at stuff in the line(s) just before line 14.

Here is some context (from your posted code):

CPP / C++ / C Code:
struct data {
   int value;
   struct data *next;
   struct data *prev;
}

/****************************************************************************/

void printArray(struct data  *iarray){ /* <=== This is line 14


So what could be happening? OH! Look! no semi-colon after the closing bracket for the struct declaration (actually line 10 in my file). Man, if I had a nickel for every time I have done that, I would have ... well, more than a nickel.

Put the semi-colon there, try another compilation and that particular error goes away.

Now, as you compile the program (and it compiles now), look for any other compiler messages.

In particular, I get:

Quote:
Warning W8075 z.c 182: Suspicious pointer conversion in function main

Now here's line 182 in my file:

CPP / C++ / C Code:
   printArray(iarray);

The line by itself looks OK, but what does it mean "Suspicious pointer conversion"? There aren't any pointers in this expression. OH! Look ...

Regards,

Dave
  #5  
Old 31-Jan-2005, 15:19
stacy12 stacy12 is offline
New Member
 
Join Date: Jan 2005
Posts: 9
stacy12 is on a distinguished road
i am using the dev-C++ compiler in the main function
printArray(iarray) is giving me this error. i put comma in line 10 but still having this error:
warning: passing arg 1 of `printArray' from incompatible pointer type

this function (printArray) print the contents of the array as initialized in the main . it recieves a pointer to the array and returns nothing.

i am also trying to create a function called make list. it's called by the mainand recieved the address of the heaptr,tailptr,and iarray and returns nothing. as follow:

read 1st value in the array
call makenode
set head and tail to the address from makenode
add new allocated node to the list
if newnode less than value contained in the first node add to the head








Quote:
Originally Posted by davekw7x
Here's a hint: If you want someone to help you understand the errors, you should list the exact error (paste from your compiler message window to the post). That lets us see what you are seeing. Different compilers have different ways of reporting errors. You could also tell us what compiler and what operating system you are using (sometimes it makes a difference).

Now, that being said, if you got a message like this, for example


That tells us (and you) that the compiler is having problems when it gets to line number 14 of your source file (in my case, the file is named z.c).

Now line 14 of my file is:

CPP / C++ / C Code:
void printArray(struct data  *iarray){

And there is nothing wrong with line 14(!)

So what now? Well, look at stuff in the line(s) just before line 14.

Here is some context (from your posted code):

CPP / C++ / C Code:
struct data {
   int value;
   struct data *next;
   struct data *prev;
}

/****************************************************************************/

void printArray(struct data  *iarray){ /* <=== This is line 14


So what could be happening? OH! Look! no semi-colon after the closing bracket for the struct declaration (actually line 10 in my file). Man, if I had a nickel for every time I have done that, I would have ... well, more than a nickel.

Put the semi-colon there, try another compilation and that particular error goes away.

Now, as you compile the program (and it compiles now), look for any other compiler messages.

In particular, I get:



Now here's line 182 in my file:

CPP / C++ / C Code:
   printArray(iarray);

The line by itself looks OK, but what does it mean "Suspicious pointer conversion"? There aren't any pointers in this expression. OH! Look ...

Regards,

Dave
  #6  
Old 31-Jan-2005, 15:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by stacy12
warning: passing arg 1 of `printArray' from incompatible pointer type



CPP / C++ / C Code:
void printArray(struct data  *iarray){

.
.
.
   printArray(iarray);

printArray is defined to take an argument of "pointer to struct data"

You call it with an argument that is "struct data".

The error message says: Argument 1 is incompatible pointer type.

What else do you need to know?

Regards,

Dave
  #7  
Old 31-Jan-2005, 16:47
stacy12 stacy12 is offline
New Member
 
Join Date: Jan 2005
Posts: 9
stacy12 is on a distinguished road
i got it

can someone helpme with the function
void makelist();

Quote:
Originally Posted by davekw7x
CPP / C++ / C Code:
void printArray(struct data  *iarray){

.
.
.
   printArray(iarray);

printArray is defined to take an argument of "pointer to struct data"

You call it with an argument that is "struct data".

The error message says: Argument 1 is incompatible pointer type.

What else do you need to know?

Regards,

Dave
  #8  
Old 03-Feb-2005, 13:21
stacy12 stacy12 is offline
New Member
 
Join Date: Jan 2005
Posts: 9
stacy12 is on a distinguished road

can someone help me


i am using the dev c++ compiler
the program running fine but there is a lot of warnings which are :


65:`addtofront' was previously implicitly declared to return `int'
88 c:\
warning: type mismatch with previous implicit declaration
43 c:\
warning: previous implicit declaration of `addtoEnd'
88 c:\
warning: `addtoEnd' was previously implicitly declared to return `int'
40 c:\
warning: previous implicit declaration of `addtofront'
64 c:\
warning: type mismatch with previous implicit declaration

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20

struct data {
   int value;
   struct data *next;
   struct data *prev;
};

//*****************************************************************************

void printArray (int  *iarray){
int a,columns=0;
for(a=0;a<SIZE;a++) {
      printf("%-8i  ",iarray[a]);
      columns++;
      if(columns%7==0)
         printf("\n");

}


}//end of function

void makeList(struct data **headptr,struct data **tailptr,int *iarray) {
int x;
struct data *newnodeptr;
 /*(newnodeptr==NULL) {
    printf("error");
    exit(0);
  } */

 for(x=0;x<SIZE;x++) {
//iarray[0];
//*headptr =( *makenode(*iarray[x]));
*tailptr = *headptr;

 if(newnodeptr<=*headptr) {
 addtofront(newnodeptr);
 }
 else if(newnodeptr>=*tailptr) {
 addtoEnd(newnodeptr);
 }
 else {
  addtocenter(newnodeptr);
 }
}
 


}

struct data *makenode(int iarray) {
 struct data *newnodeptr;
 newnodeptr = (struct data *)malloc(sizeof(struct data));
 newnodeptr->value = iarray;
 newnodeptr->next = NULL;
 newnodeptr->prev = NULL;

return (newnodeptr);
}

void addtofront(struct data *headptr, struct data *newnodeptr) {
//struct data *newnodeptr;
int iarray[20]={20,2,10,15,90,4,8,32,59,18,33,51,9,103,83,91,25,39,49,38};
//printf("n\Enter a number to the front");
//scanf("%d",&iarray[SIZE]);

 
/*newnodeptr =(struct data *)malloc(sizeof(struct data));
newnodeptr->value=iarray[0];
newnodeptr->next=NULL;

   if(headptr==NULL){ /*create empty list*/
  // headptr=newnodeptr;

   //return;
   //} */
/*add to the head of the list*/
newnodeptr->next = headptr;
headptr=newnodeptr;

puts("addition was performed");
}


void addtoEnd(struct data *tailptr){
struct data *newnodeptr, *tempnodeptr;
int x, iarray[SIZE]={20,2,10,15,90,4,8,32,59,18,33,51,9,104,83,91,25,39,49,38};
//int num
//printf("n\Enter a number to the end");
//scanf("%d",&iarray[x]);

 
newnodeptr =(struct data *)malloc(sizeof(struct data));
newnodeptr->value=iarray[0];
newnodeptr->next=NULL;

   if(tailptr == NULL){ /*create empty list*/
   tailptr=newnodeptr;

   printf("addition was performed");
   return;

   }
tailptr = tempnodeptr;
/*bring node to the end*/
  while(tempnodeptr->next) { //keep looping until last reached
  tempnodeptr=tempnodeptr->next;
  }
/*add to existing list*/
tempnodeptr->next=newnodeptr;
printf("additional performed\n");

return;
}



void addtoCenter(struct data *headptr){

struct data *currnodeptr,*newnodeptr;

currnodeptr = headptr;

while(currnodeptr!=NULL) {
  currnodeptr=currnodeptr->next;
}

newnodeptr = (struct data *)malloc(sizeof(struct data));
 newnodeptr->next = NULL;
 newnodeptr->prev = NULL;
 //newnodeptr->value = iarray[0];
 newnodeptr->prev = currnodeptr;
 newnodeptr->next = currnodeptr->next;
 currnodeptr->next->prev = newnodeptr;
 currnodeptr->next = newnodeptr;

}


void printFtoR(struct data *headptr) {
struct data *currnodeptr;

headptr=currnodeptr;
if(headptr==NULL) {
     printf("list empty");
     return;
     }

 while(currnodeptr!=NULL) {
   printf("value : %-8d\t",currnodeptr->value);
   currnodeptr = currnodeptr->next;
   return;
 }


}

void printRtoF(struct data *tailptr) {
struct data *currnodeptr;

tailptr=currnodeptr;
if(tailptr==NULL) {
     printf("list empty");
     return;
     }

 while(currnodeptr!=NULL) {
   printf("value : %-8d\t",currnodeptr->value);
   currnodeptr = currnodeptr->prev;
   return;
 }

}




void deleteEven(struct data **headptr,struct data **tailptr) {
struct data *currnodeptr,*tempnodeptr,*prevnodeptr;

currnodeptr=*headptr;

while(currnodeptr!=NULL) {
  if(currnodeptr->value%2==0) {
    if(currnodeptr==*headptr) {//front of list
      tempnodeptr=currnodeptr;
      *headptr=currnodeptr->next;
      currnodeptr=currnodeptr->next;
    }
    else {
      tempnodeptr=currnodeptr;
      prevnodeptr->next=currnodeptr->next;
      currnodeptr=currnodeptr->next;
    }
    free(tempnodeptr);
  }
  else {
  prevnodeptr=currnodeptr;
  currnodeptr=currnodeptr->next;
  }
}//end while
}//end function




//******************************************************************************


int main(void)
{

   struct data *headptr = NULL,*tailptr = NULL;
   int iarray[SIZE]={20,2,10,15,90,4,8,32,59,18,33,51,9,103,83,91,25,39,49,38};//array data

   printf("array contents:\n\n");
   printArray(iarray);
   makeList(&headptr,&tailptr,iarray);
   printf("\nordered list-front to rear\n\n");
   printFtoR(headptr);
   printf("\nordered list-rear to front\n\n");
   printRtoF(tailptr);
   deleteEven(&headptr,&tailptr);
   printf("\nordered list-front to rear with even values deleted\n\n");
   printFtoR(headptr);
   printf("\nordered list-rear to front with even values deleted\n\n");
   printRtoF(tailptr);



      //system("PAUSE");
      //return 0;

}


Can some one tell me what wrong
Last edited by LuciWiz : 26-Mar-2006 at 10:01. Reason: Please insert your C code between [c] & [/c] tags
  #9  
Old 03-Feb-2005, 13:45
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by stacy12
i am using the dev c++ compiler
the program running fine but there is a lot of warnings which are :


65:`addtofront' was previously implicitly declared to return `int'
88 c:\
warning: type mismatch with previous implicit declaration
43 c:\
warning: previous implicit declaration of `addtoEnd'
88 c:\
warning: `addtoEnd' was previously implicitly declared to return `int'
40 c:\
warning: previous implicit declaration of `addtofront'
64 c:\
warning: type mismatch with previous implicit declaration



Can some one tell me what wrong

In C, it is technically legal (but definitely not recommended) to use a function before its exact nature is known by the compiler.

When the compiler sees a function being used and it doesn't know what kind of function it is and what its arguments are: It assumes the function must return an int, and all of its arguments are int. Then later, when you actually define the function and the return type or some of the arguments are not ints, the compiler complains.

The way you tell the compiler the nature of the function is to declare it with a prototype before it is actually used anywhere in your program file.

People typically put function prototypes in header files that are #included in the program, or put protototype declarations somewhere in the scope of their usage before they are actually called.

You can get past this point simply by putting prototype declarations near the top of your file, say just after your #include statements. Code for the functions themselves can stay where it is in your file; just put in the following:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20

/* function prototypes: Note semicolon at end */
void addtofront(struct data *headptr, struct data *newnodeptr);
void addtoEnd(struct data *tailptr);
void addtoCenter(struct data *headptr);

(You have other problems, but at least you get rid of these "implicit declaration" messages, and now you know what they mean if you ever see them again --- I see them lots of times and the fix is quick.)

Regards,

Dave
  #10  
Old 03-Feb-2005, 15:09
stacy12 stacy12 is offline
New Member
 
Join Date: Jan 2005
Posts: 9
stacy12 is on a distinguished road
i am getting this error : two few arguments to function 'addtofront'

line 52:
if(newnodeptr<=*headptr) {
addtofront(newnodeptr);

what that means


Quote:
Originally Posted by davekw7x
In C, it is technically legal (but definitely not recommended) to use a function before its exact nature is known by the compiler.

When the compiler sees a function being used and it doesn't know what kind of function it is and what its arguments are: It assumes the function must return an int, and all of its arguments are int. Then later, when you actually define the function and the return type or some of the arguments are not ints, the compiler complains.

The way you tell the compiler the nature of the function is to declare it with a prototype before it is actually used anywhere in your program file.

People typically put function prototypes in header files that are #included in the program, or put protototype declarations somewhere in the scope of their usage before they are actually called.

You can get past this point simply by putting prototype declarations near the top of your file, say just after your #include statements. Code for the functions themselves can stay where it is in your file; just put in the following:

CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 20

/* function prototypes: Note semicolon at end */
void addtofront(struct data *headptr, struct data *newnodeptr);
void addtoEnd(struct data *tailptr);
void addtoCenter(struct data *headptr);

(You have other problems, but at least you get rid of these "implicit declaration" messages, and now you know what they mean if you ever see them again --- I see them lots of times and the fix is quick.)

Regards,

Dave
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 04:44.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.