Thread: doubt in strcutures

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    21

    doubt in strcutures

    hello,
    i have a doubt regarding the declaration of structures.

    i am using 2 structures both of which have head pointer one of which is local and the other one is global.how do i refer them.i tried addressing both of them as head but this gives an error.can someone please help me out.

    eg:
    Code:
    Struct A
    {
    int i;
    char *a;
    struct A *next;
    }A;
    struct A *head = NULL;(local head)
    
    struct B
    {
    char *name;
    int sign;
    B *c,*c_tail;
    struct B *next;
    }B;
    struct B *head = NULL;(global head)
    Regards,
    cutelucks

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Probably the best thing to do is simply don't give them the same name.

    Also, if English is not your first language, never use the word "doubt", because you'll never get it right!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Next time, please indent your code.
    I know of no way to access two variables of the same name in C since it generally doesn't support overloading.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by cutelucks View Post
    hello,
    i have a doubt regarding the declaration of structures.

    i am using 2 structures both of which have head pointer one of which is local and the other one is global.how do i refer them.i tried addressing both of them as head but this gives an error.can someone please help me out.
    That's not a "doubt", that's a question (although you forgot to put a ? at the end and you should put a couple spaces between each sentence).

    A "doubt" is a statement, not a question. For example:
    "I doubt I'm going to get that job I wanted." -- means you don't think you'll get the job.
    "Do you think I'll get the job I applied for?" -- This is a question that you're asking someone.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But generally it is A-OK to say "I have a doubt about <insert something here>". Depends on the context, of course.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    21
    hii,
    i am sorry for the gramatical errors.i will not repeat it in future.
    i still dint understand the answer to my question.do u mean i should use head for one and head1(or something like that for the second structure)?

    i tried using that but i get error that head1 is no known.please help me with this.

    Regards,
    cutelucks

  7. #7
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    You are struggling with the scope of the variable head.

    struct A *head = NULL;(local head)
    struct B *head = NULL;(global head)

    These two variables have the same identifier. You can get away with it if you define them inside different scope of visibility. The local head has precedence over the global. The program can see only one at a time; the local struct A *head.
    Solution: Make variables of different distinctive identifiers.
    BTW. You have many syntax errors in you structure declarations.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    But generally it is A-OK to say "I have a doubt about <insert something here>". Depends on the context, of course.
    Please don't reinforce bad English. It is very dificult to find someone who has posted on this site saying that they have "a doubt" and then proceeds to describe their actual doubt, instead of asking a question! Most people get it wrong, and you've just given them reason to care less.
    Anyway, lets stay on topic from now.

    You would have to change the declaration AND the place you use it in order to make your code compile.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubt in pointer.
    By shwetha_siddu in forum C Programming
    Replies: 5
    Last Post: 03-21-2009, 01:28 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Doubt abt Storage!
    By kalamram in forum C Programming
    Replies: 1
    Last Post: 04-21-2006, 05:30 AM
  4. basic doubt in pointer concept
    By sanju in forum C Programming
    Replies: 1
    Last Post: 10-24-2002, 11:35 PM
  5. Greatest C++ Doubt
    By vasanth in forum C++ Programming
    Replies: 15
    Last Post: 02-28-2002, 04:41 AM