Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old November 17th, 2005, 03:19 AM
Registered User
 
Join Date: Nov 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default c problems

hello i m a beginer at c language

i have a gcc compiler on suse linux 9.3
 i tried to compile following code and it did sucessfully
main()
{
int a;
scanf("%d",&a);
int num[a];
...
...
}

while i beleive that there are 2 problems
1. all initializations should be the first thing to do.
2. array declarations should only be static.

i confirmed that i do not have a c++ compiler.
please help me out.
Reply With Quote
  #2 (permalink)  
Old November 17th, 2005, 10:03 AM
Authorized User
 
Join Date: Nov 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Paramesh
Default

Hi Saurabh.
Since C is a procedure oriented language, you should do all the initializations(i.e declarations like int i; etc) before you start to use them.
and you cannot simply pass a variable to the array.
You can pass only constant values.

So, what you can do with this?
You can simply assume a value of a say 50.
Or you should use dynamic memory allocation(that will be difficult since you are a beginner).

So, for the time being, you can do like this:
Code:
int main()
{

int num[50];
...
...
return 0;
}
Note that main must always return an int.
So you should use int main at the start and return 0 in the end.

Please insert your code between the code tags.

:D
Best Regards,
Paramesh.

"Don't walk behind me; I may not lead.
 Don't walk in front of me; I may not follow.
 Just walk beside me and be my friend."
Reply With Quote
  #3 (permalink)  
Old November 19th, 2005, 09:22 AM
Registered User
 
Join Date: Nov 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sir i understand that i need to do the initializations first and only constant values need to be passed as array index what my problem is that the compiler i have is a gcc and it is allowing me to run the following code which it should not.
main()
{
int a;
scanf("%d",&a);
int temp[a];
...
...
reurn 0;
}

Reply With Quote
  #4 (permalink)  
Old November 19th, 2005, 12:49 PM
Authorized User
 
Join Date: Nov 2005
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Paramesh
Default

I think that may be the problem with the gcc compiler.
This code wont work in any other compiler and this is not a standard.


"Don't walk behind me; I may not lead.
 Don't walk in front of me; I may not follow.
 Just walk beside me and be my friend."
Reply With Quote
  #5 (permalink)  
Old November 27th, 2005, 12:55 AM
Authorized User
 
Join Date: Oct 2004
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Run gcc with the -pedantic flag to disable these non-standard features.

----
Scott J. Kleper
Author, "Professional C++"
(Wrox, 2005)
Reply With Quote
  #6 (permalink)  
Old November 29th, 2005, 02:52 AM
Registered User
 
Join Date: Nov 2005
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thank you paramesh and Mr. kleper it is a very helpful forum.

Reply With Quote
  #7 (permalink)  
Old November 29th, 2005, 04:23 AM
Authorized User
 
Join Date: Nov 2005
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Paramesh
 I think that may be the problem with the gcc compiler.
This code wont work in any other compiler and this is not a standard.
according to latest C99 standard...it is allowed...earlier it was not allowed....earlier u were only allowed to have constant value in the decleration of arrays but now its completely legal....latest compilers will conform to it.
Note: VC6.0 doesn't allow it...but most probably the new versions of it must be conforming to it...

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems already! KennySargent BOOK: Professional XNA Programming 2nd Edition ISBN: 978-0-470-26128-6 4 April 7th, 2008 03:36 AM
Have 2 problems so far... Soulcatcher BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 June 20th, 2007 12:57 PM
2 problems here with me naveed77 Beginning VB 6 1 October 17th, 2006 06:04 PM
validate.asp problems and logon.asp problems p2ptolu Classic ASP Databases 0 February 16th, 2005 02:34 PM
Problems kilika Javascript 1 October 3rd, 2003 02:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.