LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-22-2006, 10:15 AM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
const?


why cant I do this:

const int i = 1;

char *myarr[i];

I get error: variable-size type declared outside of any function

I am used to pascal and how do I get a constant in c? Do I need to use the preprocessor?
 
Old 08-22-2006, 10:25 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
that shouldn't throw any errors?
try it on it's own to see if it is this that is causing the error.
Quote:
int main()
{
const int i = 1;
char *myarr[i];
}
 
Old 08-22-2006, 10:36 AM   #3
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
That works but I want it outside a function.

like this.

const int i = 1;
char *myarr[i];

int main()
{
return 0;
}


and then I get error: variable-size type declared outside of any function
 
Old 08-22-2006, 10:40 AM   #4
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
Maybe: const char *myarr[i];

noop did not work.


I want to write genetric code. I need an array of widgets so I can add currencies in the future just by adding the name and/or number. I do not want to go in to the code and add a widget here and there.

Last edited by kalleanka; 08-22-2006 at 10:44 AM.
 
Old 08-22-2006, 10:41 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
I don't see a problem with that either??
VS doesn't have a problem.I'll just boot up nix an and see if it compiles.

[edit]This seems to be C specific problem (I missed that in your first post), I didn't know of a difference in C and C++ in this respect. hmmm.

Last edited by dmail; 08-22-2006 at 10:50 AM.
 
Old 08-22-2006, 10:52 AM   #6
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
Well gcc givs:

Desktop]$ gcc -Wall test.c -o test
test.c:2: error: variable-size type declared outside of any function

I think this is strange.


But this works

#define i 1
const char *myarr[i];
int main()
{
return 0;
}


now I only have to figuerout how I can use these in diffrent files.
 
Old 08-22-2006, 10:55 AM   #7
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
You are probebly rigth that it works in C++. Its bigger diffencies between the two than I thougth now when I learnt some c.
 
Old 08-22-2006, 11:05 AM   #8
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
http://www.linuxtopia.org/online_boo...ter08_005.html

RRR now I remember all the #defines I used in C.
If the value is needed for more than just the array size then use a define (but change the name from i to array_size or something). Else just char * myarr[1];

Last edited by dmail; 08-22-2006 at 11:09 AM.
 
Old 08-22-2006, 11:09 AM   #9
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by kalleanka
#define i 1
const char *myarr[i];
int main()
{
return 0;
}
the reason it works is because it is no longer a variable size array, when you compile i is replaced with 1. so what you have there is the same to the compiler as saying

const char *myarr[1];

while what you had originally seems like it should work (i though so anyhow), i just looked it up and it is not c standard to declare a variable size array globally like that.
Quote:
ISO C - 6.7.5.2
Only an ordinary identifier (as defined in 6.2.3) with both block scope or function
prototype scope and no linkage shall have a variably modified type.
 
Old 08-22-2006, 11:10 AM   #10
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
Thanks dmail
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
c++: const member won't compile Garda Programming 2 07-19-2006 03:55 AM
C++: #define OR const int ? Hady Programming 5 10-07-2005 11:43 AM
Non-Const reference? ChemicalBurn Programming 4 04-05-2005 08:32 AM
const in C++ lazyboy0001 Programming 5 04-08-2004 04:43 PM
Saving an Ip to a Const? Half_Elf Linux - Networking 3 07-12-2002 08:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration