passing pointers to other files

This is a discussion on passing pointers to other files within the C Programming forums, part of the General Programming Boards category; I'm having trouble passing pointers from a c file to a header file and still being able to call information. ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    passing pointers to other files

    I'm having trouble passing pointers from a c file to a header file and still being able to call information.
    I assume My mistakes are trivial but any help would be greatly appreciated

    .c file
    Code:
    #include <ctype.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include "test.h"
    
    typedef struct
    {
    	char type[20];
    	char value[20];
    }Lexeme;
    
    int main(int argc, char *argv[])
    {
        int scanner(char*, Lexeme *lexeme_pointer);
        Lexeme *lexeme_pointer;
    
    	//scanner(argv[1], lexeme_pointer);////to be implemented later
    	scanner("readme.txt", lexeme_pointer);  
    	system("PAUSE");
    	return 0;
    }
    int scanner(char* filename, Lexeme *lexeme_pointer)
    {
    	lexInit(filename);
    	lex(lexeme_pointer);	
    	return 0;
    }
    .h file
    Code:
    FILE *readfile;
    int lexInit(char* filename)
    {
     readfile = fopen(filename,"r");
     if(readfile == 0)
     {
      printf("\nfile could not be opened\n\n");
     }
     else
     {
      printf("\n File opened\n");	
     }
     return 0;
    }
    
    int lex(Lexeme *lexeme_pointer)
    {
    	char ch;
    	ch = readchar();
    	lexeme_pointer->type = 'funny';
    	printf("type = %s",&lexeme_pointer.type);
    
    
     return 0;
    }
    
    ///readchar
    int readchar()
    {
     char ch;
      ch = fgetc(readfile);
      return ch;
    }
    int pushback()
    {
    
    }
    int newLexeme(Lexeme *lexeme_pointer)//input string, return a lexeme structure type:NUMBER,VARIABLE data:3, main
    {
    	lexeme_pointer = (Lexeme*)malloc(sizeof(Lexeme));
    	if(lexeme_pointer == 0)
    	{
    		printf("Out of Memory");
    	}
     
    }
    don't worry about the readme file.
    It can be literally any file.
    My main problem is with the passing of Lexeme *lexeme_pointer to and from the header file and maintaining the values

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    well, you see, people don't put actuall code inside header files, headers are used to put function prototypes, global variables, data structures and other stuff your prog uses there. and you don't pass any pointers from .h to .c
    :wq

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    so should I change this to another .c file?
    I am trying to maintain a fairly clean file for my main scanner function file.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Among other things...
    Code:
    lexeme_pointer->type = 'funny';
    Are you trying to copy a string here?
    Code:
    strcpy(lexeme_pointer->type, "funny");
    You should be using an int here:
    Code:
    int readchar(void)
    {
     char ch; /* should be an int */
      ch = fgetc(readfile);
      return ch;
    }
    and here
    Code:
    int lex(Lexeme *lexeme_pointer)
    {
        char ch; /* should be an int */
        ch = readchar();
    And this...
    Code:
    printf("type = %s",&lexeme_pointer.type);
    I believe this should be as follows.
    Code:
    printf("type = %s", lexeme_pointer->type);
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    I appreciate your help, I am very rusty in C.
    but I think that ch should stay a char because I am reading in characters one at a time from a file.
    However, my revised code is crashing
    Any additional help and/or tips would be appreciated
    thanks

    Basically my code is supposed to do this:
    It reads from a file, and every character it crosses, it returns the name.
    So say it reads {*}
    output should be: OBRACKET
    ASTERISK
    CBRACKET
    here is my main source file
    Code:
    #include <ctype.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    extern int lexInit(char* filename);
    extern int lex();
    extern int lexemeDisplay();
    
    int main(int argc, char *argv[])
    {
        int scanner(char*);
    	
        
    
    	//scanner(argv[1]);////to be implemented later
    	scanner("readme.txt");  
    	system("PAUSE");
    	return 0;
    }
    int scanner(char* filename)
    {
    	char lexeme;
    
    	lexInit(filename);
    	lexeme = lex();
    	lexemeDisplay(lexeme);
    	printf("lexeme = %s",lexeme);///print test
    	/*while(lexeme-> != EndofInput)
    	{
    		lexemeDisplay(lexeme);
    		lexeme = lex();
    	}*/
    	return 0;
    }


    here is the function file
    Code:
    #include <ctype.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    #define OBRACE "OBRACE"
    #define CBRACE "CBRACE"
    #define OBRACKET "OBRACKET"
    #define CBRACKET "CBRACKET"
    #define OPAREN "OPAREN"
    #define COLON "COLON"
    #define SEMICOLON "SEMICOLON"
    #define OCTOTHORPE "OCTOTHORPE"
    #define ASTERISK "ASTERISK"
    #define PIPE "PIPE"
    #define FORWARDSLASH "FORWARDSLASH"
    #define BACKSLASH "BACKSLASH"
    #define PLUS "PLUS"
    #define PLUSPLUS "PLUSPLUS"
    #define MINUS "MINUS"
    #define MINUSMINUS "MINUSMINUS"
    #define EQUALS "EQUALS"
    #define EQUALSEQUALS "EQUALSEQUALS"
    
    
    typedef struct
    {
    	char type;
    	char value;
    }Lexeme;
    FILE *readfile;
    
    int lexInit(char* filename)
    {
    	readfile = fopen(filename,"r");
    	if(readfile == 0)
    	{
    		printf("\nfile could not be opened\n\n");
    	}
    	else
    	{
    		printf("\n File opened\n");
    	}
    	return 0;
    }
    
    int lex()
    {
    	Lexeme *lexeme_pointer;
    	char ch;
    	
    	ch = readchar();
    	printf("ch = %c\n",ch);//print test
    	switch(ch)
    	{
    	case '{': return newLexeme(OBRACE);
    		break;
    	case '}': return newLexeme(CBRACE);
    		break;
    	case '[': return newLexeme(OBRACKET);
    		break;
    	case ']': return newLexeme(CBRACKET);
    		break;
    	case '"': return newLexeme(OPAREN);
    		break;
    	case ':': return newLexeme(COLON);
    		break;
    	case ';': return newLexeme(SEMICOLON);
    		break;
    	case '#': return newLexeme(OCTOTHORPE);
    		break;
    	case '*': return newLexeme(ASTERISK);
    		break;
    	case '|': return newLexeme(PIPE);
    		break;
    	case '/': return newLexeme(FORWARDSLASH);
    		break;
    	case '\\': return newLexeme(BACKSLASH);
    		break;
    		/////need more cases///////////
    	case '+': ch = readchar();
    		if(ch == '+')
    		{
    			return newLexeme(PLUSPLUS);
    		}
    		else
    		{
    			pushback(ch);
    			return newLexeme(PLUS);
    		}
    		break;
        case '-': ch = readchar();
    		if(ch == '-')
    		{
    			return newLexeme(MINUSMINUS);
    		}
    		else
    		{
    			pushback(ch);
    			return newLexeme(MINUS);
    		}
    		break;
    	case '=': ch = readchar();
    		if(ch == '=')
    		{
    			return newLexeme(EQUALSEQUALS);
    		}
    		else
    		{
    			pushback(ch);
    			return newLexeme(EQUALS);
    		}
    		break;
    	}	
    	
    }
    
    ///readchar
    int readchar()
    {
    	char ch;
    	ch = fgetc(readfile);
    	return ch;
    }
    ///pushback
    int pushback()//needs to pushback the character that was read back on the stack
    {
    	return 0;
    }
    ////newlexeme
    int newLexeme(char *type)//input string, return a lexeme structure type:nNUMBER,VARIABLE data:3, main
    {
    	Lexeme *pointer;
    	
    	printf("type = %s\n",type);//////////////print test//////////////////	
    	pointer = (Lexeme*)malloc(sizeof(Lexeme));
    	if(pointer == 0)
    	{
    		printf("Out of Memory");
    	}
    	pointer->type = type;
    	return pointer->type;
    }
    ///lexemeDisplay
    int lexemeDisplay(char *lexeme)
    {
    	printf("lexeme = %s\n",lexeme);///////print test////////////////
    }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by AmazingRando
    but I think that ch should stay a char because I am reading in characters one at a time from a file.
    Well, you're wrong.

    Quote Originally Posted by AmazingRando
    Any additional help and/or tips would be appreciated
    You can't return strings.

    You need to declare or define functions before calling them.

    Code:
    ////newlexeme
    int newLexeme(char *type)//input string, return a lexeme structure type:nNUMBER,VARIABLE data:3, main
    {
        Lexeme *pointer;
        
        printf("type = %s\n",type);//////////////print test//////////////////    
        pointer = (Lexeme*)malloc(sizeof(Lexeme));
        if(pointer == 0)
        {
            printf("Out of Memory");
        }
        pointer->type = type;
        return pointer->type;
    }
    [Error 64] Type mismatch (assignment) (char = pointer)
    There still a lot more, but I'm signing off.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    I appreciate all your help

    in this portion of code, I assume I should return the pointer instead.

    Code:
    int newLexeme(char *type)//input string, return a lexeme structure type:nNUMBER,VARIABLE data:3, main
    {
    	Lexeme *pointer;
    	
    	printf("1.type = %s\n",type);//////////////print test//////////////////	
    	pointer = (Lexeme*)malloc(sizeof(Lexeme));
    	if(pointer == 0)
    	{
    		printf("Out of Memory");
    	}
    	pointer->type = type;
    	return pointer;
    }
    But I'm still getting indirection warnings on both of these lines
    Code:
    pointer->type = type;
    	return pointer;
    what would be the best way to fix this in your opinion?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 10-15-2008, 04:08 AM
  2. parameter passing and pointers
    By pokiepo in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 01:13 AM
  3. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Passing variables by pointers...what am I doing wrong?
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 06-07-2002, 03:10 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21