Reputation: 45
I'm second year (learning phase T_T). We learn structures now and I decided to try headers. But they seem to do not want to work, too lazy or something :D
The problem is that the 'Vector' structure which is defined in header file init.h which is included in the main.c for futher use. Was thinking everything is cool but error occured! Damn, it highlightes Vector* vStart;
line (THE ERROR LINE). Well after some reasearch of that error I have found that it's very general error which occur in either the structure or header related cases.
Error code: a label can only be part of a statement and a declaration is not a statement
Example:
init.h
#ifndef INIT_H
#define INIT_H
#define vecLength 4
typedef struct Vector {
double * vector;
int N;
} Vector;
typedef struct Matrix {
double ** matrix;
int nRow;
int nCol;
} Matrix;
int matrixInit(Matrix* nMatrix);
int vectorInit(Vector* nVector);
#endif // INIT_H
main.c
#include "init.h"
main(){
...
...
switch(_char)
{
case Start:
Vector* vStart;
if(vectorInit(vStart)){
getStartPoint(vStart);
vectorPrint(vStart);
}
else{
hFe("Vector vStart is not created!");
return 1;
}
getch();
break;
case Translation:
hFe(NULL);
return 1;
case Exit:
return 0;
default:
system("cls");
goto AGAIN;
}
}
Upvotes: 2
Views: 1258
Reputation: 16540
A label must be on a separate line, in general starting in the first column of the line, and have nothing else on the line. I.E.
mylabel:
Upvotes: -1
Reputation: 213306
Assuming you are using a somewhat modern C compiler, the error is not at all related to scope as the present answers suggest.
Like the compiler says: "error: a label can only be part of a statement and a declaration is not a statement". The error merely comes from incorrect label syntax. case
follows the syntax rules of labels, the syntax must be like this (6.8.1):
labeled-statement:
identifier : statement
case constant-expression : statement
default : statement
Meaning a label must be followed by a statement, not a declaration or something else that isn't regarded as a statement in C. So you get the same compiler error as you would get if attempting something like goto label; label: int x;
One way to dodge the compiler error is simply to add an empty statement:
case Start:
;
Vector* vStart;
That being said, you might still want a local scope for each case by adding braces: doing so is good practice.
Looking at the greater picture however, it doesn't seem like it even makes sense to declare vStart
in a local scope anyhow. You should declare it at the beginning of main and initialize it to a safe value, for example NULL.
Upvotes: 3
Reputation: 16213
Change your switch
case
to
case Start:
{
Vector* vStart;
if(vectorInit(vStart))
{
getStartPoint(vStart);
vectorPrint(vStart);
}
else
{
hFe("Vector vStart is not created!");
return 1;
}
getch();
}
break;
In this way, with brackets, you create a scope inside the case where you can declare variables. BTW you should declare it at the top of your function to make a readable code.
Upvotes: 1