Question Authors' Forum

C-programming - data definition has no type or storage class [-Werror]

C-programming - data definition has no type or storage class [-Werror]

by Jens Allmer -
Number of replies: 8

Hi,

this is probably easy, but I cannot seem to find the solution. I would like to test a C function written by the students.

I have the following code as the question answer: 

char* getArrVZ() {
    char arrvz[42];
    arrvz[0]='v';
    for(int i=1; i<42; i++) {
        if(i%2 == 0)
            arrvz[i] = 'v';
        else
            arrvz[i] = 'z';
    }
    return arrvz;
}
In the test cases I have for instance:
printf("%c",getArrVZ()[0]);
and check whether the result is the expected 'v'.
While this works outside of Cr, I get errors when trying to save the question:
__tester__.c:9:5: error: data definition has no type or storage class [-Werror]
    9 |     arrvz[0]='v';
      |     ^~~~~
__tester__.c:9:5: error: type defaults to ‘int’ in declaration of ‘arrvz’ [-Werror=implicit-int]
__tester__.c:9:5: error: conflicting types for ‘arrvz’
__tester__.c:8:10: note: previous declaration of ‘arrvz’ was here
How to solve this?
Thank you

In reply to Jens Allmer

Re: C-programming - data definition has no type or storage class [-Werror]

by Matthew Toohey -

Hi Jens

What question type are you using (c_function or c_program)?

When I try using the c_function question type with your code I run into a different error which is "__tester__.c:18:12: error: function returns address of local variable [-Werror=return-local-addr]". This is because your function is returning a local array defined in the function (which is no longer safe to use once the function has returned). It seems that either the function should take a preallocated array to fill as an argument or the function should use malloc to allocate space for the array before returning it.


Matthew

In reply to Matthew Toohey

Re: C-programming - data definition has no type or storage class [-Werror]

by Jens Allmer -

Hi Matthew,

I am using c_function as the question type (attached as moodle xml).
Once I'll get to the error you encounter (which makes sense), I'll be happy to use malloc to fix it ;)

At the moment the errors are not making sense to me (see below).

Thanks,
Jens

The complete error trace is below:

Syntaxfehler

__tester__.c:9:5: error: data definition has no type or storage class [-Werror]
    9 |     arrvz[0]='v';
      |     ^~~~~
__tester__.c:9:5: error: type defaults to ‘int’ in declaration of ‘arrvz’ [-Werror=implicit-int]
__tester__.c:9:5: error: conflicting types for ‘arrvz’
__tester__.c:8:10: note: previous declaration of ‘arrvz’ was here
    8 |     char arrvz[42];
      |          ^~~~~
__tester__.c:9:14: error: invalid initializer
    9 |     arrvz[0]='v';
      |              ^~~
__tester__.c:10:5: error: expected identifier or ‘(’ before ‘for’
   10 |     for(int i=1; i<42; i++) {
      |     ^~~
__tester__.c:10:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
   10 |     for(int i=1; i<42; i++) {
      |                   ^
__tester__.c:10:25: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘++’ token
   10 |     for(int i=1; i<42; i++) {
      |                         ^~
__tester__.c:16:5: error: expected identifier or ‘(’ before ‘return’
   16 |     return arrvz;
      |     ^~~~~~
__tester__.c:17:1: error: expected identifier or ‘(’ before ‘}’ token
   17 | }
      | ^
__tester__.c: In function ‘main’:
__tester__.c:19:29: error: implicit declaration of function ‘getArrVZ’ [-Werror=implicit-function-declaration]
   19 | int main() {   {printf("%c",getArrVZ()[0]);;
      |                             ^~~~~~~~
__tester__.c:19:39: error: subscripted value is neither array nor pointer nor vector
   19 | int main() {   {printf("%c",getArrVZ()[0]);;
      |                                       ^
__tester__.c:7:38: error: expected ‘)’ before ‘char’
    7 | #define SEPARATOR "##"char* getArrVZ() {
      |                                      ^~~~
__tester__.c:20:20: note: in expansion of macro ‘SEPARATOR’
   20 |    }printf("%s\n", SEPARATOR);   {printf("%c",getArrVZ()[11]);;
      |                    ^~~~~~~~~
__tester__.c:31:1: error: expected declaration or statement at end of input
   31 | }
      | ^
cc1: all warnings being treated as errors
@17943918#@>

In reply to Jens Allmer

Re: C-programming - data definition has no type or storage class [-Werror]

by Matthew Toohey -

Unfortunately I have been unable to replicate your error. I imported the question you provided and it worked (apart from the aforementioned error). I was able to get a very similar error trace to what you observed if the answer was set to just the function body like the following.

    char arrvz[42];
    arrvz[0]='v';
    for(int i=1; i<42; i++) {
        if(i%2 == 0)
            arrvz[i] = 'v';
        else
            arrvz[i] = 'z';
    }
    return arrvz;

Otherwise I'm not sure what could be going wrong.

In reply to Matthew Toohey

Re: C-programming - data definition has no type or storage class [-Werror]

by Richard Lobb -

Like Matthew, I'm unable to replicate the error. When I import your question and run it, I get just the error that Matthew mentioned in his first posting.

Looking at your error output I think the key error is:

_tester__.c:7:38: error: expected ‘)’ before ‘char’
    7 | #define SEPARATOR "##"char* getArrVZ() {

This shows that the template code

#define SEPARATOR "#<ab@17943918#@>#"

{{ STUDENT_ANSWER }}

has been expanded by Twig but has somehow had the tag-like substring <@17943918#@> stripped and has had the white space collapsed. The collapsing of white space appears elsewhere in your error messages, too.

I've never seen this behaviour before but it looks like it is some special Twig behaviour. Twig does have a striptags filter that does something very similar (deletes tags, collapses whitespace) but it applies only within a Twig expression. I'm not aware of any global setting that causes Twig to behave like that throughout the entire template.

So ... do you have any idea why your system should be behaving in this anomalous way? If not, can you tell me a bit about your Moodle/CodeRunner install, please? Specifically:

  1. What version of Moodle are you running and is it a Linux-based server or a Windows-based server?
  2. When was CodeRunner installed on this Moodle server (i.e. is it a recent addition or an old one)?
  3. Has CodeRunner worked OK in the past?

Also, just to confirm that my diagnosis is correct, could you include the source code output from the run in your response, please? It should be as follows but I think it will actually look like it has been rendered as HTML.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
s#include <string.h>
#include <stdbool.h>
#include <math.h>
#define SEPARATOR "#<ab@17943918#>#"

char* getArrVZ() {
    char arrvz[42];
    arrvz[0]='v';
    for(int i=1; i<42; i++) {
        if(i%2 == 0)
            arrvz[i] = 'v';
        else
            arrvz[i] = 'z';
    }
    return arrvz;
}

int main() {
   {
    printf("%c",getArrVZ()[0]);;
   }
    printf("%s\n", SEPARATOR);   {
    printf("%c",getArrVZ()[11]);;
   }
    printf("%s\n", SEPARATOR);   {
    char t = getArrVZ()[42];
if(t == 'v' || t == 'z')
printf("bad");
else
print("good");;
   }
    printf("%s\n", SEPARATOR);   {
    for(int i=0; i<42; i+=2)
printf("%c",getArrVZ()[i]);
   }
    printf("%s\n", SEPARATOR);   {
    for(int i=1; i<42; i+=2)
printf("%c",getArrVZ()[i]);
   }
        return 0;
}
In reply to Richard Lobb

Re: C-programming - data definition has no type or storage class [-Werror]

by Jens Allmer -
Hi,
I tried for a while but I couldn't get it to work.
Then I created a new question, filled the needed fields, and tried again.
The final moodle product is attached.
Please note that I fixed my code as well.

The result with your code is:
v#<ab@17943918#>#
z#<ab@17943918#>#
good#<ab@17943918#>#
vvvvvvvvvvvvvvvvvvvvv#<ab@17943918#>#
zzzzzzzzzzzzzzzzzzzzz
Using the standard code instead of what you provided, I get the errors below.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define SEPARATOR "#<ab@17943918#@>#"

{{ STUDENT_ANSWER }}
int main() {
{% for TEST in TESTCASES %}
   {
    {{ TEST.testcode }};
   }
    {% if not loop.last %}printf("%s\n", SEPARATOR);{% endif %}
{% endfor %}
    return 0;
}

Errors:
Syntaxfehler
__tester__.c:9:19: error: initializer element is not constant
    9 |     char *arrvz = malloc( sizeof(char) * 43 );
      |                   ^~~~~~
__tester__.c:10:5: error: data definition has no type or storage class [-Werror]
   10 |     arrvz[0]='v';
      |     ^~~~~
__tester__.c:10:5: error: type defaults to ‘int’ in declaration of ‘arrvz’ [-Werror=implicit-int]
__tester__.c:10:5: error: conflicting types for ‘arrvz’
__tester__.c:9:11: note: previous definition of ‘arrvz’ was here
    9 |     char *arrvz = malloc( sizeof(char) * 43 );
      |           ^~~~~
__tester__.c:10:14: error: invalid initializer
   10 |     arrvz[0]='v';
      |              ^~~
__tester__.c:11:5: error: expected identifier or ‘(’ before ‘for’
   11 |     for(int i=1; i<42; i++) {
      |     ^~~
__tester__.c:11:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘<’ token
   11 |     for(int i=1; i<42; i++) {
      |                   ^
__tester__.c:11:25: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘++’ token
   11 |     for(int i=1; i<42; i++) {
      |                         ^~
__tester__.c:17:5: error: data definition has no type or storage class [-Werror]
   17 |     arrvz[42] = '\0';
      |     ^~~~~
__tester__.c:17:5: error: type defaults to ‘int’ in declaration of ‘arrvz’ [-Werror=implicit-int]
__tester__.c:17:5: error: conflicting types for ‘arrvz’
__tester__.c:9:11: note: previous definition of ‘arrvz’ was here
    9 |     char *arrvz = malloc( sizeof(char) * 43 );
      |           ^~~~~
__tester__.c:17:17: error: invalid initializer
   17 |     arrvz[42] = '\0';
      |                 ^~~~
__tester__.c:18:5: error: expected identifier or ‘(’ before ‘return’
   18 |     return arrvz;
      |     ^~~~~~
__tester__.c:19:1: error: expected identifier or ‘(’ before ‘}’ token
   19 | }
      | ^
__tester__.c: In function ‘main’:
__tester__.c:21:29: error: implicit declaration of function ‘getArrVZ’ [-Werror=implicit-function-declaration]
   21 | int main() {   {printf("%c",getArrVZ()[0]);;
      |                             ^~~~~~~~
__tester__.c:21:39: error: subscripted value is neither array nor pointer nor vector
   21 | int main() {   {printf("%c",getArrVZ()[0]);;
      |                                       ^
cc1: all warnings being treated as errors

In reply to Jens Allmer

Re: C-programming - data definition has no type or storage class [-Werror]

by Richard Lobb -

I notice that both questions you have sent us are customised and the second question has template code that is not in fact a Twig template at all. So I think you might be misunderstanding how CodeRunner works and in particular the role of the template field and the Twig template preprocessor.  For simple "write a function" questions you should not need to customise the question, nor deal with templates.

I attach an XML export of a question that I believe is roughly what you want. Please import this, preview it, click "Fill in correct responses" (or the German equivalent) and then Check. What happens?

Note that the answer that the student is expected to supply is the entire function declaration, including the function header, not just the body.

Before attempting to customise questions I suggest you read the documentation sections The Architecture of CodeRunner and Question types.

[Edit: sorry, forgot to include attachment. Added now.]

In reply to Richard Lobb

Re: C-programming - data definition has no type or storage class [-Werror]

by Jens Allmer -

Thank you, 

I could not find the attached file you mentioned.

Unfortunately, this is the only moodle installation I have access to, and it is in German, so I don't know whether twig template and "Anpassung - Vorlage" refer to the same thing (likely, though).

While I did customize questions before, this one was left unchanged by me, and it is what shows up when I click new question .. type Cr, and then click on customize. If the below does not meet the expectations for a standard question, there might be something wrong with the installation. 

If you can confirm that the below is not as expected, it would be great to have the standard twig code as a quick fix. I'll contact the admins about it so that it might be fixed.

Thank you,

Jens



#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define SEPARATOR "#<ab@17943918#@>#"
{{ STUDENT_ANSWER }}
int main() {
{% for TEST in TESTCASES %}
   {
    {{ TEST.testcode }};
   }
    {% if not loop.last %}printf("%s\n", SEPARATOR);{% endif %}
{% endfor %}
    return 0;
}
In reply to Jens Allmer

Re: C-programming - data definition has no type or storage class [-Werror]

by Richard Lobb -

The template is fine.

Sorry about the missing attachment. I've now added it to the post. Could you import it, preview it, click Fill in correct response and then click Check, please? If it doesn't work, please email me (richard.lobb@canterbury.ac.nz) directly, so we can try to work out what's going wrong. I think we should take further discussion off line until we've found out what the problem is.