Ex26: goto label executes always

Hello everyone,
here is my code:

void get_comp_regex(regex_t *reg, size_t nreg, char *argv[]){
7 int i = 1;
8 int rc = 0;
9
10 // check if the -o flag was given
11 if (!strncmp(argv[1], “-o”,2)){
12
13 // or
14 debug(“nreg: %li”,nreg);
15 for (i = 2; i <= nreg; i++){
16 rc = regcomp(&reg[i-1],argv[i],0);
17 if (rc == 0) {debug(“rc == 0”);}
18 check(rc == 0, “Compilation of regex failed.”);
19 }
20
21 }else{
22 // and
23 }
24
25 error: {printf(“error label\n”); exit(1);}
26 }
27
28
29
30 int main(int argc, char *argv[]){
31
32 regex_t regex[argc-1];
33 size_t nreg = argc-1;
34
35 get_comp_regex(regex,nreg,argv);
36
37 return 0;
}

I run: ./ex26 -o hello

OUTPUT:
DEBUG ex26.c:14: nreg: 2
DEBUG ex26.c:17: rc == 0
error label

if I comment out the check in line 18 I get the “warning: label ‘error’ defined but not used” but the label executes also and I get the same output.

It should not execute in both cases, right? In the first case the check is true so it should not jump, in the second there is no jump statment.
All help is very much appreciated! Thank you!

You can jump to labels but they are not control statements themselves. Everything below a label gets executed unless you bail out before. A label is just a name for a line of code, nothing more.

printf("%d ",0);
first:  printf("%d ",1);
second: printf("%d ",2);
return;
third:  printf("%d\n ",3);

This code in some function will print 0 1 2 no matter the labels.


When you post here, if you wrap your code in code tags like so

[code]
# your code
[/code]

it is easier to read.

Thank you for your very fast answer! I’ll make sure to use the code tags in the future!
You saved my day! :slight_smile:

A free service run by Zed A. Shaw for learncodethehardway.org.