The declaration here makes no mistake in C

The next line has the problem int (*f)(int, int) = (argv[2][0] == 'd') , when compiling it says that the declaration is not allowed here. If a string is declared from the very beginning, any better way to do this. Any suggestions would be greatly appreciated?

 #include <stdio.h> #include <stdlib.h> #include <ctype.h> int encode(int ch, int key) { if (islower(ch)) { ch = (ch-'a' + key) % 26 + 'a'; ch += (ch < 'a') ? 26 : 0; } else if (isupper(ch)) { ch = (ch-'A' + key) % 26 + 'A'; ch += (ch < 'A') ? 26 : 0; } return ch; } int decode(int ch, int key) { return encode(ch, -key); } int main(int argc, char **argv) { int ch; int key; if (argc < 2) { printf("USAGE: cipher <integer key> <encode | decode>\n"); printf("Then, just type your text and it will automatically output the en/de crypted text! :)\n"); return 1; } key = atoi(argv[1]); if (key < 1 || key > 25) { printf("Key is invalid, or out of range. Valid keys are integers 1 through 25.\n"); return 1; } int (*f)(int, int) = (argv[2][0] == 'd') ? decode : encode; while (EOF != (ch=getchar())) putchar(f(ch, key)); return 0; } 
+4
source share
4 answers

In C (before C99) you must declare variables at the beginning of the block.

Compile your code as C99 or change the code so that f declared at the beginning of the block.

+11
source

In c89 / 90 you have to declare all variables when starting the block

But in c99, you can compile your code with -std=c99 as follows:

gcc -Wall -std=c99 test.c -o test.out

+2
source

In addition to the part indicated by NPE, you can use typedef to create the type of the function. eg:

typedef void FunctionType (int, int); And then use it (as a separate type) to create function pointers.

Easy to read.

+2
source

If the line will be declared at the beginning

In C89, definitions must be performed before any statements in a block. If you move it, you do not need to move the entire line (and, of course, you do not want to move the entire line before the code that checks argv[2] checked). Just move the definition of f :

  int ch; int key; int (*f)(int,int); ... f = (argv[2][0] == 'd') ? decode : encode; 

any better way to do it

This is not necessarily better in this case, but note that this rule is the beginning of a block, not necessarily the beginning of a function.

So you can simply write:

 { int (*f)(int, int) = (argv[2][0] == 'd') ? decode : encode; while (EOF != (ch=getchar())) putchar(f(ch, key)); } return 0; 

You can easily delve into the arguments about this coding style. Some people think that every function should define all its variables in front, and introducing a block just to define a variable is cluttered and / or confusing. Some people (and especially those who use C ++, as well as C) believe that you should limit the scope of each variable to as narrow a piece of code as possible, that defining everyone at the beginning of a function is cluttered and / or confusing. But even they may consider excessive blocking excessive.

+2
source

All Articles