Sep 19, 2013 #1 E electronics_guy Member level 2 Joined Apr 12, 2012 Messages 49 Helped 0 Reputation 0 Reaction score 0 Trophy points 1,286 Activity points 1,655 Hi all, What is wrong with the snippet: Code: const int a=5; int b[a]={1,2,3,4,5}; The compiler always throws an error with the code above. What could be the problem? Plz help Thanks in advance
Hi all, What is wrong with the snippet: Code: const int a=5; int b[a]={1,2,3,4,5}; The compiler always throws an error with the code above. What could be the problem? Plz help Thanks in advance
Sep 19, 2013 #2 Venkadesh_M Advanced Member level 4 Joined Jun 26, 2013 Messages 1,374 Helped 258 Reputation 516 Reaction score 254 Trophy points 1,363 Location Coimbatore, India Activity points 8,020 you can not use a variable as array size but It may be a macro Code C - [expand]1 2 3 #define a 5 int b[a]={1,2,3,4,5}; already a is constant by this method you are saving a variable space also....
you can not use a variable as array size but It may be a macro Code C - [expand]1 2 3 #define a 5 int b[a]={1,2,3,4,5}; already a is constant by this method you are saving a variable space also....
Sep 19, 2013 #3 kripacharya Banned Joined Dec 28, 2012 Messages 1,204 Helped 182 Reputation 360 Reaction score 175 Trophy points 1,343 Location New Delhi Activity points 0 Logically it seems that it should work. But i suppose it depends on your compiler how its handled. You could instead "#define a" if you know what it is at compile time.....
Logically it seems that it should work. But i suppose it depends on your compiler how its handled. You could instead "#define a" if you know what it is at compile time.....
Sep 19, 2013 #4 Venkadesh_M Advanced Member level 4 Joined Jun 26, 2013 Messages 1,374 Helped 258 Reputation 516 Reaction score 254 Trophy points 1,363 Location Coimbatore, India Activity points 8,020 It is not compiler dependent It is c standard in all the compilers.
Sep 19, 2013 #5 errakeshpillai Full Member level 5 Joined Oct 17, 2011 Messages 255 Helped 64 Reputation 128 Reaction score 59 Trophy points 1,328 Location Chennai, India Activity points 2,738 Many compilers like GCC will allow to use this, but the array should not be initialized. Its values can be changed/assigned afterwards in the program.
Many compilers like GCC will allow to use this, but the array should not be initialized. Its values can be changed/assigned afterwards in the program.
Sep 19, 2013 #6 Venkadesh_M Advanced Member level 4 Joined Jun 26, 2013 Messages 1,374 Helped 258 Reputation 516 Reaction score 254 Trophy points 1,363 Location Coimbatore, India Activity points 8,020 In ubuntu GCC Attachments error.JPG 53.2 KB · Views: 145 working.JPG 39.7 KB · Views: 153 gcc.JPG 46.5 KB · Views: 126 Last edited: Sep 19, 2013
Sep 19, 2013 #7 kripacharya Banned Joined Dec 28, 2012 Messages 1,204 Helped 182 Reputation 360 Reaction score 175 Trophy points 1,343 Location New Delhi Activity points 0 Venkadesh_M said: It is not compiler dependent It is c standard in all the compilers. Click to expand... Which C standard are you referring to ? I did not realise that all compilers behaved in exactly the same way..... That would surely obviate the need for any variety in C compilers.
Venkadesh_M said: It is not compiler dependent It is c standard in all the compilers. Click to expand... Which C standard are you referring to ? I did not realise that all compilers behaved in exactly the same way..... That would surely obviate the need for any variety in C compilers.
Sep 19, 2013 #8 Venkadesh_M Advanced Member level 4 Joined Jun 26, 2013 Messages 1,374 Helped 258 Reputation 516 Reaction score 254 Trophy points 1,363 Location Coimbatore, India Activity points 8,020 I have compiled in ubutu GCC as well for more info refer this discussion https://stackoverflow.com/questions/13645936/variably-modified-array-at-file-scope-in-c
I have compiled in ubutu GCC as well for more info refer this discussion https://stackoverflow.com/questions/13645936/variably-modified-array-at-file-scope-in-c