case error with linked list

This is a discussion on case error with linked list within the C Programming forums, part of the General Programming Boards category; I'm having an error when using a linked list. I'm simply trying to have a list where you can create...add...delete..etc... ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    49

    case error with linked list

    I'm having an error when using a linked list. I'm simply trying to have a list where you can create...add...delete..etc...

    But in my case statement I receive.....

    Code:
    initialization of 'l1' is skipped by 'case' label
    I've never seen this before, any help?

    here's my code...
    Code:
            choice=menu();
            switch(choice)
                {
                case 1:
                lnklst l1;
                l1.create();
    thanks
    Code this

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Code:
                {
                case 1:
                lnklst l1;
                l1.create();
    Your var declaration is pushed to the start of the enclosing block, past the label.
    You have in effect
    Code:
                {
                lnklst l1;  // oops, can't reach this.
                case 1:
                l1.create();
    I suspect if you want your list to have any kind of lifetime (longer than the switch basically), it needs to be declared in some outer scope.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Also, you're either having fun with function pointers, or you're actually writing C++. If you can't understand variable scope, I'd lay off the function pointers for a while and focus on the basics. Otherwise, I'd post on the appropriate board.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-05-2009, 10:32 AM
  2. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21