2

I'm trying to compare elements being fed in from a text file into my 2D array. An example of the textfile is as follows:

ABCDE
FGHIK
LMNOP

I know I can read in the file fine and can print each element as well.

I'm trying to go through and compare each character with the next. In my head this is how it works: If they match, move on to the next char, then compare that char with the next. If they do not match, then print "error" to the terminal and break out of the switch statement. Though when I run this in my program it only prints "Is this being accsessed?" "It sure is". Any help on what I could be doing wrong would be very helpful.

void match_char(char** array, int height, int width){
    int a, i, j;
    printf("Is this being accessed?");
    a = 0;
    i = 0;
    j = 0;
    switch(a){
        case 1: if(a <1 ){
            for(j = 0; j < width; j++){
                if (array[i][j] != array[i][j+1]){
                    printf("error\n");
                    break;
                } else if(j == width) {
                    a=1;
                    break;
                }
            }
        }/* End of case 1*/
        case 2: if (a > 0 && a < 2){
            for(i = 0; i < height; i++){
                if (array[i][j] != array[i+1][j]){
                    printf("error\n");
                    break;
                }else if(i == height) {
                    printf("No error");
                    a=2;
                    break;
                }
            }
        }/* End of case 2 */
        case 3: 
            if (a > 2) {
                a=0;
                break;
            }
    }/*End of switch */

    printf("It sure is");

}
5
  • 1
    It's not clear what you are teying to find. Commented Aug 21, 2014 at 14:58
  • 1
    Your use of the switch statement is wrong: cases 1 and 2 will never be executed. Commented Aug 21, 2014 at 14:58
  • 3
    a is intialised with the value 0. As there is no case 0 or a default in the switch,it skips the switch since none of the cases are true Commented Aug 21, 2014 at 15:00
  • It looks like a will always be 0 when you enter the switch, and there is no 0 case. What exactly is a representing? It looks like there are some other logic problems in the switch. Commented Aug 21, 2014 at 15:00
  • @andrewlamb, my thought process is that I can use that to switch between the cases. Commented Aug 21, 2014 at 15:03

2 Answers 2

3

What Case Means

Just to be clear, in case X, the X is not some label for that case, it's a check of whether the Y in switch(Y) equals X.

To help visualize this, let's rewrite the switch as an if... else statement.

a = 0; 
if(a == 1){ //  == case 1
    // stuff
} else if(a == 2){ // == case 2
    // stuff
} else { // == default
...

Those if statements you have in the switch right now have NO bearing on whether that case is executed.


How Switch works

Now, let's talk about how a switch works. When the code is compiled, depending on the range of values for each case, either a jump table or a something virtually identical to a if... else if... else statement is generated. Regardless, you have a list of instructions. The pseudo-assembly looks like this:

instruction n : multiply the value of a by some offset and jump to a*offset + n
n +a*offset_1 : do stuff 
                jump to m // break
n +a*offset_2 : do stuff
                jump to m // break
n +a*offset_3 : do stuff
                jump to m // break
n +a*offset_4 : do stuff
                jump to m // break
instruction m : do more stuff

If you leave off the breaks,

instruction n : multiply the value of a by some offset and jump to a*offset + n
n +a*offset_1 : do stuff 
n +a*offset_2 : do stuff
n +a*offset_3 : do stuff
n +a*offset_4 : do stuff
instruction m : do more stuff

So you can see why the breaks prevent the rest of the cases from being executed - and leaving them out makes that case and every one after it execute, regardless of whether or not you meet the case condition.

Sign up to request clarification or add additional context in comments.

1 Comment

@user3510340 having a visual image of the background to the C itself helps a lot. Also, do remember Fred's comment on Luis's answer; breaks within for loops are distinct from a break a case has.
1

in the first case label you ask if a < 1 after getting there with a == 1 so the if is always false.

in the second case, a == 2, so it cannot be equal to 1 (the if checks for a > 0 and a < 2, so it can only be equal to 1 to get into the if)

in the third case you ask for a > 2 (always, as you get there when a == 3) so all the if conditions must be redefined properly.

I suppose you know that you initialize a = 0 and have no case for that value of a, so you are just skipping the switch without entering any of the cases.

You have also not used break; sentences, i suppose purposely, but as you don't have any case for a == 0, you'll never get into any of the switch cases.

2 Comments

Also, the break statements just exit the for loops, and do not keep control flow from continuing on to the next case clause.
thank you, still trying to understand how to properly use a switch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.