Simple Question

This is a discussion on Simple Question within the C Programming forums, part of the General Programming Boards category; Code: #include<stdio.h> int main() { int a,&b=a; a = 10; printf("\na = %d",a); printf("\nb = %d",b); b = 15; printf("\na ...

  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
    20,980
    That looks like invalid syntax, at least for C.
    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

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,182
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  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
    20,980
    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.
    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

  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, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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