Thread: Simple Question

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    66

    Simple Question

    Code:
    #include<stdio.h>
    
    int main() {
       int a,&b=a;
       a = 10;
       printf("\na = %d",a);
       printf("\nb = %d",b);
       b = 15;
       printf("\na = %d",a);
       printf("\nb = %d",b);
       return 0;
    }
    Does this code mean : Address of b is address of a?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That looks like invalid syntax, at least for C.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is that a reference? Seeing as pointers can be defined that was, I suppose references are possible, too, but I have never seen it and I can't test right now...
    But it definitely isn't C. More like C++ in that case.
    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
    Registered User
    Join Date
    Jan 2008
    Posts
    66
    I don't know for sure, but it does compile and give me outputs as if a and b are aliases.
    So this is not a valid C code then?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I don't know for sure, but it does compile and give me outputs as if a and b are aliases.
    You probably compiled the code as C++. C does not have C++ style references.

    So this is not a valid C code then?
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    hai

    Quote Originally Posted by laserlight View Post
    You probably compiled the code as C++. C does not have C++ style references.


    Yes.
    Hello

    Absolutely it is not C programming feature..it is the C++ feature..you are creating Reference for the variable..also called Alieas of variable you are creating..in your case the output
    would be:-

    a=10
    b=10
    a=15
    b=15

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ThLstN View Post
    Code:
    #include<stdio.h>
    
    int main() {
       int a,&b=a;
       a = 10;
       printf("\na = %d",a);
       printf("\nb = %d",b);
       b = 15;
       printf("\na = %d",a);
       printf("\nb = %d",b);
       return 0;
    }
    Does this code mean : Address of b is address of a?
    Also, aside from it not being valid C, there is no necessity for the compiler to actually store the address of a in b - all that is REQUIRED by the compiler is that it make b behave like another name of a. So it may even just say "store a in Register1, and b is also found in Register1", for the purpose of the above code.

    In more complex cases, yes, then b would contain the address of the variable that it's referencing (which also means that the referenced variable can not be stored in a register).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM