Thread: C2061 errors : declaration not being recognised?

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    C2061 errors : declaration not being recognised?

    edit: solved

    Some background. I've been programming a few years and am generally quite good at thinking in programming terms. I've done C (ansi), java, haskell, and a lot of VHDL recently. I am, however, somewhat rusty on my C so while I understand it fairly well there is always the chance of a stupid blind error.

    Currently I am (you have permission to shoot me) using visual studio and the microsoft compiler, simply because a previous program i wrote used 3rd party DLLs that were only compatible with the microsoft compiler. I have no intention of keeping multiple compilers on the same computer so I intend to get used to what is unfortunately the standard now.

    The section of code I'm having trouble with is one that should be simple. I've already cleaned up the usual typos and obvious mistakes. The function is just a quick throwtogether for parsing a file into lines (partly as a utility, partly to convert my head from vhdl to C again ready for the rest of the program).

    Here is the first half of the program containing the faulty function:
    Code:
    /*
     * Util.c
     * Version 1.0
     * misc useful functions
     */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    typedef struct A {
    	char* line;
    	struct A* next;
    } strlink;
    
    /* reads a file into lines, max length 200 char */
    char** parseLines( char* filename )
    {
    	FILE* stream = NULL;
    	char c = 0;
    	char* temp = NULL;
    	char** out = NULL;
    	strlink* start = NULL;
    	strlink* cur = NULL;
    	int i = 0;
    	int j = 0;
    	stream = fopen( filename, "r");
    	if( !stream )
    		return NULL;
    	
    	start = calloc( 1, sizeof( strlink ));
    	cur = start;
    
    	/*all is initialised, now parse it into links in a linked list */
    	do while( c != EOF )
    	{
    		temp = calloc( 200, sizeof( char ));
    		j = 0;
    		c = fgetc(stream);
    		while( j < 200 && c != '\n' && c != EOF)
    		{	
    			temp[j++] = c;
    			c = fgetc(stream);
    		}
    		cur->line = temp;
    		cur->next = calloc( 1, sizeof( strlink ));
    		cur = cur->next;
    		i++;
    	}
    	
    	/* build output array from linked list, output is null terminated */
    	out = calloc( i+1, sizeof ( char* ));
    	cur = start;
    	for( j = 0 ; j < i ; j++ )
    	{
    		out[j] = cur->line;
    		start -> cur;
    		cur = cur->next;
    		free(start);
    	}
    	fclose( stream );
    	return out;
    }
    the compiler output, ive got a quick batch i use to remember the tags for me: these have worked fine for me in the past

    >cl quests.c util.c /link /OUT:quest.exe
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 15.00.21022.08 for 80x86
    Copyright (C) Microsoft Corporation. All rights reserved.

    quests.c
    util.c
    util.c(52) : error C2061: syntax error : identifier 'out'
    Generating Code...

    line 52 is the line
    Code:
    	out = calloc( i+1, sizeof ( char* ))
    ;

    since out is a char**, the allocation is an array of strings, and the brackets line up I'm running out of possible causes. Any insight would be greatly appreciated
    Last edited by tritous; 08-09-2009 at 04:58 AM. Reason: problem solved

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    60
    what a loooooooooong intro ...

    Code:
    out = (char**) calloc( i+1, sizeof ( char* ))
    p/s: i'm not good at thinking or programming....

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    background is useful. my biggest problem is I lock strongly into whatever language I'm currently using and it takes time to get back into the C mindset. I used to know the whole definition from the tvalues system to most of the standard libraries from memory, but now I keep thinking "signal data : std_logic_vector(7 downto 0)" which is rather depressing. I can't believe I forgot calloc returned void*, I've typecasted it (to be sure) throughout now, cheers for that.

    It hasn't solved the problem, but at least my code is more formal now so that's one possible cause discounted.

    updated code:

    Code:
    /*
     * Util.c
     * Version 1.0
     * misc useful functions
     */
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    
    typedef struct A {
    	char* line;
    	struct A* next;
    } strlink;
    
    /* reads a file into lines, max length 200 char */
    char** parseLines( char* filename )
    {
    	FILE* stream = NULL;
    	char c = 0;
    	char* temp = NULL;
    	char** out = NULL;
    	strlink* start = NULL;
    	strlink* cur = NULL;
    	int i = 0;
    	int j = 0;
    	stream = fopen( filename, "r");
    	if( !stream )
    		return NULL;
    	
    	start = (strlink*) calloc( 1, sizeof( strlink ));
    	cur = start;
    
    	/*all is initialised, now parse it into links in a linked list */
    	do while( c != EOF )
    	{
    		temp = (char*) calloc( 200, sizeof( char ));
    		j = 0;
    		c = fgetc(stream);
    		while( j < 200 && c != '\n' && c != EOF)
    		{	
    			temp[j++] = c;
    			c = fgetc(stream);
    		}
    		cur->line = temp;
    		cur->next = (strlink*) calloc( 1, sizeof( strlink ));
    		cur = cur->next;
    		i++;
    	}
    	
    	/* build output array from linked list, output is null terminated */
    	out = (char**) calloc( i+1, sizeof ( char* ));
    	cur = start;
    	for( j = 0 ; j < i ; j++ )
    	{
    		out[j] = cur->line;
    		start = cur;
    		cur = cur->next;
    		free(start);
    	}
    	fclose( stream );
    	return out;
    }
    Last edited by tritous; 08-09-2009 at 04:35 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This code that comes just before that line is suspect:
    Code:
    do while( c != EOF )
    {
        /* ... */
    }
    For that code to be syntactically valid, it should be along these lines:
    Code:
    do
        while( c != EOF )
        {
            /* ... */
        }
    while (/* ... */);
    You probably wanted to write:
    Code:
    do
    {
        /* ... */
    } while( c != EOF );
    Next, my compiler tells me that this is suspect:
    Code:
    start -> cur;
    start is a pointer to a strlink, but a strlink does not have a member variable named cur. Even if it did, that line would have no net effect.

    bvkim, it is not necessary to cast the return value of calloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    sorry, my fault for posting in the middle of a change. I did fix the start -> cur fault

    The do while was the fault, thanks laserlight. Do loops were always something I avoided in the past so that's probably why I got it wrong.

    Problem solved . Now I can look for semantic bugs :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Replies: 6
    Last Post: 09-27-2007, 07:17 AM
  3. Ten Errors
    By AverageSoftware in forum Contests Board
    Replies: 0
    Last Post: 07-20-2007, 10:50 AM
  4. errors initializing D3D
    By Vacation Guy in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2005, 12:20 PM
  5. please help me
    By insane in forum Game Programming
    Replies: 8
    Last Post: 05-12-2003, 12:40 AM