-1

I am new in C and I don't know where problem is. I am making some program and I want in case of a1 to change value of string a1 and display message (I know that I can organize chars, I will do that later). I am getting two errors, first one is:

[Error] case label does not reduce to an integer constant - Line 96

and second

[Error] incompatible types when assigning to type 'char[60]' from type 'char *' - Line 96

code:

#include <stdio.h>
char a1[60];

    switch(trenutni)
        case a1: printf("A1 je %s",a1="Brazil");
}
else
    printf("Nemate vise poteza, vise srece drugi put!/nBroj bodova koje ste osvojili:%");   

3 Answers3

2

The line

case a1:

does not compile because

  1. a1 is a variable and the value is not know at 'compile time'.

  2. a1 is a char[] which is not allowed in C

A case label must be an expression which is known at compile time i.e a constant or a constant expression. In your case you have to transform the switch to an if:

if(strcmp(trenutni, a1) == 0) ...
DrKoch
  • 9,556
  • 2
  • 34
  • 43
1

The issues you are having are related to data type mismatch.

First,

switch(trenutni)
  case a1:

a1 refers to the declared variable, which is an array, which a switch does not support. Your C program is expecting an integer or character instead of an array, like this:

switch ( trenutni )
  case 'a': printf...

Guessing from your code, it appears that you want the user to be able to enter "a1" to the console, in which case, alternatively, you could refactor to use an if-train if you want to test against values longer than a single character:

if ( strcmp( trenutni, "a1" ) == 0 )

Second,

a1="Brazil"

is another type mismatch. Assignments need to be of the same type; char[] and char* are not the same type; you can see so in the error message. See this SO answer about the differences between char* and char[].

Here is a potential refactor of the code with some comments (of course, this is based on a guess of what you are trying to accomplish):

#include <stdio.h>

    char trenutni[3];
    char *a1;

    int main() {
            printf("Unesite polje koje zelite da otvorite!");
            fflush(stdout);
            fgets(trenutni, 3, stdin); // read in a string 

            if ( strcmp(trenutni, "a1") == 0 ) { // compare string against "a1"
                a1 = "Brazil";
                printf("A1 je %s", a1); // keep the assignment and the output on separate lines
            }
    }
Community
  • 1
  • 1
jzheaux
  • 7,042
  • 3
  • 22
  • 36
  • 1
    1) `trenutni` will never legitimately compare equal to `"a1"`, because the latter contains three characters, whereas the former contains room for only two. 2) `scanf("%s", ...` is unsafe, use `fgets()`. 3) You're missing an `fflush(stdout);` after the first `printf()` call. – Crowman Apr 20 '15 at 21:10
  • @PaulGriffiths 1) This was not my experience with the original code, though when I added your suggestions in 2 and 3, I saw it behave as you described, I've updated it accordingly. 2 & 3) Thanks, I've edited my answer to follow your suggestions. – jzheaux Apr 20 '15 at 21:26
0

The line

printf("A1 je %s",a1="Brazil");

Is rather bogus. Don't combine a printf and an asignment (which itself is bogus). I think you wanted simnply:

printf("A1 je %s", a1);

because a1 is a char[] you may set a new value like so:

strcpy(a1, "Brazil");
DrKoch
  • 9,556
  • 2
  • 34
  • 43
  • This is a very unsafe use of `strncpy()`. If `a1` is less than 7 characters long, this will leave it unterminated. – Crowman Apr 20 '15 at 21:13