Basic question about declaring variables/structures

This is a discussion on Basic question about declaring variables/structures within the C Programming forums, part of the General Programming Boards category; Is there any difference between doing following declarations ? If so what are they? any benefits at all ? Thanks ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    7

    Basic question about declaring variables/structures

    Is there any difference between doing following declarations ? If so what are they? any benefits at all ?

    Thanks in advance.

    #1:
    Code:
    main() { 
       int i = 1; 
       struct my_struct *s = NULL;  
    }
    #2:
    Code:
    main() { 
       int i = 1; 
       if(i) {
          struct my_struct *s = NULL;  
       } 
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did they teach you about variable scope yet? If so, and you've forgotten, or if not, you should go back and read that section again.


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

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    #2 has the profound disadvantage that at some point in your program you're going to get an error that the struct was never declared.

    Whoever told you that was a good idea?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Whoever told you that was a good idea?
    The same people that told you the other day that scattering variable declarations around was good.


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

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    7
    doh! *thud* head hits desk. heh, thanks.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    The same people that told you the other day that scattering variable declarations around was good.


    Quzah.
    Hey... watch which way you pointing that "Point of View" gun!!!

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    After all, he opened up a new scope to declare the variable!

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Hey... watch which way you pointing that "Point of View" gun!!!
    I was just pointing out the humor in you doing what he was doing, and then asking him who told him that doing what you were doing was a good idea.


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

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I was just pointing out the humor in you doing what he was doing, and then asking him who told him that doing what you were doing was a good idea.


    Quzah.

    Well, there is a difference ... When I do it, it works.

    I create mid-function scopes very deliberately and never declare variables anywhere but the very top of a scope.

    It's as close to nested functions as you're gonna get in C.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I wasn't debating if it was deliberate or not. I assumed this was a simple exercise to teach scope. You both were doing the same exact thing, so I found it amusing that you were frowning upon the act you were partaking in.


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

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I wasn't debating if it was deliberate or not. I assumed this was a simple exercise to teach scope. You both were doing the same exact thing, so I found it amusing that you were frowning upon the act you were partaking in.
    Quzah.
    Oh, I see... well, doc yourself 3 points and carry on... LOL.

    EDIT: In my own defense, I never told him not to do it... only that doing it that way wouldn't work.
    Last edited by CommonTater; 05-19-2011 at 04:54 PM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,438
    Quote Originally Posted by CommonTater
    In my own defense, I never told him not to do it... only that doing it that way wouldn't work.
    Bad defense though, since doing it that way works when it makes sense and is done correctly, hence your own claim that "when I do it, it works".

    Quote Originally Posted by bnyec
    Is there any difference between doing following declarations ? If so what are they? any benefits at all ?
    In this case the examples are so trivial and contrived and it makes no real difference either way, and the program has no net effect anyway. So, the difference is purely an educational one concerning scope, as quzah outlined to you in post #2. In a more complete program, the difference/benefit depends on the bigger picture of what you are trying to do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Declaring structures inside structures ?
    By jamaican1231 in forum C Programming
    Replies: 6
    Last Post: 04-13-2010, 03:40 PM
  2. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  3. Question about variables in structures
    By Donarstan in forum C Programming
    Replies: 1
    Last Post: 03-23-2008, 04:43 AM
  4. Declaring Variables for Arrays~ Newbie Question
    By sashax415 in forum C++ Programming
    Replies: 2
    Last Post: 12-17-2005, 04:27 PM
  5. question about declaring global variables
    By Dag_ in forum C++ Programming
    Replies: 2
    Last Post: 04-27-2005, 06:03 AM

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