Thread: switch/case - Just wondering

  1. #1
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89

    switch/case - Just wondering

    I was just wondering wether there are any reasons why I shouldn't do something like this:

    Code:
    void func(int p) {
      switch(p) {
        case 1: {
          int i;
          /* Do something with i */
        } break;
    
        case 2: {
          char msg[10];
          /* Do something with msg */
        } break;
      }
    }
    instead of

    Code:
    void func(int p) {
      int i;
      char msg[10];
    
      switch(p) {
        case 1:
          /* Do something with i */
          break;
        case 2:
          /* Do something with msg */
          break;
      }
    }
    So basicly, is it alright to place brackets in every case and then declare variables in it? Because I'm learning the win32 API at the moment and I don't like it when I have to declare all of my variables at the top of the WndProc function, especially with big programs it's annoying...

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Quote Originally Posted by Snip
    I was just wondering wether there are any reasons why I shouldn't do something like this:

    Code:
    void func(int p) {
      switch(p) {
        case 1: {
          int i;
          /* Do something with i */
        } break;
    
        case 2: {
          char msg[10];
          /* Do something with msg */
        } break;
      }
    }
    instead of

    Code:
    void func(int p) {
      int i;
      char msg[10];
    
      switch(p) {
        case 1:
          /* Do something with i */
          break;
        case 2:
          /* Do something with msg */
          break;
      }
    }
    So basicly, is it alright to place brackets in every case and then declare variables in it? Because I'm learning the win32 API at the moment and I don't like it when I have to declare all of my variables at the top of the WndProc function, especially with big programs it's annoying...
    The first one only creates the variables if the case is met, which saves you from creating them if you don't need them.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's a problem with declaring your variables in a case statement. I believe Salem has discussed it here before. I can't quite remember what it was, or actually who all was discussing it. Well, aside from the fact that the variable's scope only exists for that case block.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    void func(int p) {
      switch(p) {
        case 1: {
          int i;
          /* Do something with i */
        } break;
    
        case 2: {
          char msg[10];
          /* Do something with msg */
        } break;
      }
    }
    This is fine - the scope of the variables is limited by the braces.
    Though for the reasons you state, you should consider making each case a separate function to keep the body of the switch nice and simple. One recently memorable (for the wrong reasons) switch was way over 3000 lines long.

    The problem Quzah is thinking of is this
    Code:
    void func(int p) {
      switch(p) {
        int badvar = 0;
        case 1: {
          int i;
          /* Do something with i */
        } break;
    
        case 2: {
          char msg[10];
          /* Do something with msg */
        } break;
      }
    }
    The problem is, despite what it looks like, badvar will never be initialised (cases are labels, and the switch is an organised goto). But the variable will still exist non the less.

    gcc for example reports
    warning: unreachable code at beginning of switch statement
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wondering if this would be a dangerous use of globals.
    By suzakugaiden in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 10:51 PM
  2. Alternative to switch/case?
    By chrisjmoss in forum C Programming
    Replies: 1
    Last Post: 01-16-2006, 11:40 AM
  3. wondering about people here
    By moemen ahmed in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 07-08-2002, 09:33 PM
  4. Just wondering.
    By n3urai in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-20-2002, 08:11 PM
  5. Just wondering
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 02-13-2002, 12:14 AM