• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 249
  • Last Modified:

How to pass an array as a parameter to another function?

I have this function that reads one line from a text file.   The file is a list of tokens from a previous scanner program.  I need to classified the tokens into their catergories.  I can't figure out how to use the result from this function and pass it to the other function that compares the tokens and classifies them into their category.  Can anyone help me out.

char getToken()
{
   int SIZE = 40;
   char line[ SIZE ];
 
   if(( tokfile = fopen( "tokens.txt", "r" )) != NULL )
   {
      if( fgets( line, 40, tokfile ) == NULL )
         printf( "fgets error\n" );
      else
         printf( "%s", line );
   }
   
   return line[ SIZE ];

}
0
morales7_0
Asked:
morales7_0
  • 2
1 Solution
 
jkrCommented:
You unfortunately cannot do it that way. The idea would be to

char* getToken()
{
  int SIZE = 40;
  char* line= new char[ SIZE ];

   if(( tokfile = fopen( "tokens.txt", "r" )) != NULL )
  {
     if( fgets( line, 40, tokfile ) == NULL )
        printf( "fgets error\n" );
     else
        printf( "%s", line );
  }
   
  return line;

}

And, of course, you need to 'delete[]' the returned value when you no longer need it. Another approach would be to

void getToken(char* line )
{
  int SIZE = 40;
  char line[ SIZE ];

   if(( tokfile = fopen( "tokens.txt", "r" )) != NULL )
  {
     if( fgets( line, 40, tokfile ) == NULL )
        printf( "fgets error\n" );
     else
        printf( "%s", line );
  }
   
  return line[ SIZE ];

}

and use it like

  int SIZE = 40;
  char line[ SIZE ];

  getToken(line);
 
0
 
morales7_0Author Commented:
Cool!!!  How would I compare the result from getToken() function with the list of categories in my tokenTable() function?    
0
 
jkrCommented:
>>with the list of categories in my tokenTable() function?

Sorry, but since I do not know that function, it is hard to give any advice on that :o)
0
 
SirHandoCommented:
You need to pass the address...

char getToken()
{
   int SIZE = 40;
   char line[ SIZE ];
 
   if(( tokfile = fopen( "tokens.txt", "r" )) != NULL )
   {
      if( fgets( &line, 40, tokfile ) == NULL )
         printf( "fgets error\n" );
      else
         printf( "%s", line );
   }
   
   return line[ SIZE ];
}

I do not know the point of returning line[SIZE] here either?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now