We help IT Professionals succeed at work.

Mystified by error message

GPicasso
GPicasso asked
on
Hey I have been debugging this code all day and am unable to figure out my error messages.  everything but the main program will work.  
I have included all the code and the error messages.  I need to have this at least print out using a list.  Any help is appreciated.
 error-messages
/*fatal.h*/
#include <stdio.h>
#include <stdlib.h>

#define Error( Str )        FatalError( Str )
#define FatalError( Str )   fprintf( stderr, "%s\n", Str ), exit( 1 )

Open in new window

/*list.h*/       

 typedef int ElementType;

        #ifndef _List_H
        #define _List_H

        struct Node;
        typedef struct Node *PtrToNode;
        typedef PtrToNode List;
        typedef PtrToNode Position;

        List MakeEmpty( List L );
        int IsEmpty( List L );
        int IsLast( Position P, List L );
        Position Find( ElementType X, List L );
        void Delete( ElementType X, List L );
        Position FindPrevious( ElementType X, List L );
        void Insert( ElementType X, List L, Position P );
        void DeleteList( List L );
        Position Header( List L );
        Position First( List L );
        Position Advance( Position P );
        ElementType Retrieve( Position P );

        #endif

Open in new window

#include <stdio.h>
#include <stdlib.h>
#include "list.h"
/*main*/

void print_list(List root) {
int i = 0;  

while (root) {
    printf("%c ", Find(i, root));
    i++;
  }
  printf("\n");
}
/*
Node* reverse(Node* root) {
  Node* new_root = 0;
  while (root) {
    Node* next = root->next;
    root->next = new_root;
    new_root = root;
    root = next;
  }
  return new_root;
}*/

List reverse(List* root){
List new_root;
int x = First(*root);
int y;
int i = 1;
while (root){
new_root = x;
y = Find(i, root);
x = y;
i++;
}
return new_root;

int main() {
List* root;
Insert(1, root, 0);
Insert(2, root, 1);
Insert(3, root, 2);
Insert(4, root, 3);
  /*Node d = { 'd', 0 };
  Node c = { 'c', &d };
  Node b = { 'b', &c };
  Node a = { 'a', &b };

  Node* root = &a;*/
  print_list(root);
  root = reverse(root);
  print_list(root);

  return 0;
}

Open in new window

/*list.c*/
#include "list.h"
	#include <stdlib.h>
	#include "fatal.h"

	
	struct Node
	{
	ElementType Element;
	Position    Next;
	};

	List
	MakeEmpty( List L )
	{
	if( L != NULL )
	DeleteList( L );
	L = malloc( sizeof( struct Node ) );
	if( L == NULL )
	FatalError( "Out of memory!" );
	L->Next = NULL;
	return L;
	}


	int
	IsEmpty( List L )
	{
	return L->Next == NULL;
	}


	int IsLast( Position P, List L )
	{
	return P->Next == NULL;
	}

	Position
	Find( ElementType X, List L )
	{
	Position P;

	      P = L->Next;
	      while( P != NULL && P->Element != X )
	          P = P->Next;

	     return P;
	}
	


	void
	Delete( ElementType X, List L )
	{
	Position P, TmpCell;

	P = FindPrevious( X, L );

	if( !IsLast( P, L ) )  
	{                     
	TmpCell = P->Next;
	P->Next = TmpCell->Next;  
	free( TmpCell );
	}
	}


	Position
	FindPrevious( ElementType X, List L )
	{
	Position P;

	      P = L;
	      while( P->Next != NULL && P->Next->Element != X )
	          P = P->Next;

	      return P;
	}
	

	void
	Insert( ElementType X, List L, Position P )
	{
	Position TmpCell;

	      TmpCell = malloc( sizeof( struct Node ) );
	      if( TmpCell == NULL )
	          FatalError( "Out of space!!!" );

	      TmpCell->Element = X;
	      TmpCell->Next = P->Next;
	      P->Next = TmpCell;
	}
	

	


	void
	DeleteList( List L )
	{
	Position P, Tmp;

	     P = L->Next;  
	      L->Next = NULL;
	      while( P != NULL )
	{
	         Tmp = P->Next;
	          free( P );
	          P = Tmp;
	}
	}
	

	Position
	Header( List L )
	{
	return L;
	}

	Position
	First( List L )
	{
	return L->Next;
	}

	Position
	Advance( Position P )
	{
	return P->Next;
	}

	ElementType
	Retrieve( Position P )
	{
	return P->Element;
	}

Open in new window

Comment
Watch Question

mccarlIT Business Systems Analyst / Software Developer
CERTIFIED EXPERT
Top Expert 2015

Commented:
Should line 39 of your TestProb1.c file (the one containing the reverse and main functions) have an end brace? Just after the

return new_root;

and before the start of your main() function.
CERTIFIED EXPERT
Commented:
To make your program compile you need to add the closing "}" to the
function: List reverse(List* root).

This will compile, but will not produce correct results due to several mistakes in your code.
- uninitialized variables
- incompatible types of variables

------------------ compiler warnings -------------
testProb1.c: In function 'reverse':
testProb1.c:29: warning: initialization makes integer from pointer without a cast
testProb1.c:33: warning: assignment makes pointer from integer without a cast
testProb1.c:34: warning: passing argument 2 of 'Find' from incompatible pointer type
list.h:16: note: expected 'List' but argument is of type 'struct Node **'
testProb1.c:34: warning: assignment makes integer from pointer without a cast
testProb1.c: In function 'main':
testProb1.c:43: warning: passing argument 2 of 'Insert' from incompatible pointer type
list.h:19: note: expected 'List' but argument is of type 'struct Node **'
testProb1.c:44: warning: passing argument 2 of 'Insert' from incompatible pointer type
list.h:19: note: expected 'List' but argument is of type 'struct Node **'
testProb1.c:44: warning: passing argument 3 of 'Insert' makes pointer from integer without a cast
list.h:19: note: expected 'Position' but argument is of type 'int'
testProb1.c:45: warning: passing argument 2 of 'Insert' from incompatible pointer type
list.h:19: note: expected 'List' but argument is of type 'struct Node **'
testProb1.c:45: warning: passing argument 3 of 'Insert' makes pointer from integer without a cast
list.h:19: note: expected 'Position' but argument is of type 'int'
testProb1.c:46: warning: passing argument 2 of 'Insert' from incompatible pointer type
list.h:19: note: expected 'List' but argument is of type 'struct Node **'
testProb1.c:46: warning: passing argument 3 of 'Insert' makes pointer from integer without a cast
list.h:19: note: expected 'Position' but argument is of type 'int'
testProb1.c:53: warning: passing argument 1 of 'print_list' from incompatible pointer type
testProb1.c:6: note: expected 'List' but argument is of type 'struct Node **'
testProb1.c:54: warning: assignment from incompatible pointer type
testProb1.c:55: warning: passing argument 1 of 'print_list' from incompatible pointer type
testProb1.c:6: note: expected 'List' but argument is of type 'struct Node **'
testProb1.c:43: warning: 'root' is used uninitialized in this function
--------------------------------------------------------------------------------

------------------ splint warnings ------------------------------------------
testProb1.c: (in function print_list)
testProb1.c:10:19: New fresh storage (type Position) passed as implicitly temp
                      (not released): Find(i, root)
  A memory leak has been detected. Storage allocated locally is not released
  before the last reference to it is lost. (Use -mustfreefresh to inhibit
  warning)
testProb1.c: (in function reverse)
testProb1.c:29:9: Variable x initialized to type Position, expects int:
                     First(*root)
  Types are incompatible. (Use -type to inhibit warning)
testProb1.c:33:2: Assignment of int to List: new_root = x
testProb1.c:34:2: Assignment of Position to int: y = Find(i, root)
testProb1.c:38:8: Variable new_root used before definition
  An rvalue is used that may not be initialized to a value on some execution
  path. (Use -usedef to inhibit warning)
testProb1.c: (in function main)
testProb1.c:43:11: Variable root used before definition
testProb1.c:43:17: Null storage passed as non-null param: Insert (..., 0)
  A possibly null pointer is passed as a parameter corresponding to a formal
  parameter with no /*@null@*/ annotation.  If NULL may be used for this
  parameter, add a /*@null@*/ annotation to the function parameter declaration.
  (Use -nullpass to inhibit warning)
testProb1.c:44:17: Function Insert expects arg 3 to be Position gets int: 1
testProb1.c:45:17: Function Insert expects arg 3 to be Position gets int: 2
testProb1.c:46:17: Function Insert expects arg 3 to be Position gets int: 3
testProb1.c:57:12: Fresh storage root not released before return
   testProb1.c:54:3: Fresh storage root created
testProb1.c:6:6: Function exported but not used outside testProb1: print_list
  A declaration is exported, but not used outside this module. Declaration can
  use static qualifier. (Use -exportlocal to inhibit warning)
   testProb1.c:14:1: Definition of print_list
testProb1.c:27:6: Function exported but not used outside testProb1: reverse
   testProb1.c:39:1: Definition of reverse
---------------------------------------------------------------------------------------


List reverse(List* root){
List new_root;
int x = First(*root);
int y;
int i = 1;
while (root){
new_root = x;
y = Find(i, root);
x = y;
i++;
}
return new_root;
} /* <--- THIS was missing */

Open in new window

General strategy comment.  Try to address the errors in the order they appear.

testProb1.c:29: warning: initialization makes integer from pointer without a cast

int x = First(*root);

Open in new window


First returns a Position (which is a pointer to Node).  You are trying to assign it to an integer and that is almost never a good idea.  This might work better.

Position x = First ( *root );

Open in new window




Michael EagerConsultant
Commented:
If you indent code to reflect its structure, it is much easier to read and understand.  In particular, it becomes much easier to identify missing or misplaced parenthesis or braces.   This would have made your missing brace error in reverse() obvious.  

You can use vi to automatically highlight mismatched quotes, parens, or braces.  It will also do auto-indent.

@eager -- I think it depends on which vi you use.  If vi is linked to vim, I get highlighting.  If vi is linked to elvis, I don't.

@GPicasso -- Here's an article on indentation    http://en.wikipedia.org/wiki/Indent_style

I personally use Allman style.  This is probably because I learned Pascal before I learned C.  (Pascal, if I recall correctly, use BEGIN and END instead of { and }).  Whichever style you choose, I suggest you try to be consistent.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.