![]() |
|
#1
|
|||
|
|||
? re strings as character arrays functionI can't figure out why I'm getting compiler errors for the following code:
CPP / C++ / C Code:
Quote:
|
|
#2
|
|||
|
|||
Re: ? re strings as character arrays functionWhen you declare an array do you do this,
CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: ? re strings as character arrays functionQuote:
Quote:
|
|
#4
|
|||
|
|||
Re: ? re strings as character arrays functionActually, never mind, the book does say:
Quote:
|
|
#5
|
|||
|
|||
Re: ? re strings as character arrays functionSo in this rudimentary phase of programming (without dynamic structures) there's no way to change the size of an array with a variable, even when first initializing it? That seems a bit restrictive - I can see how changing the size of an array after initializing it could wreak havoc, but how could initializing with a variable cause a problem?
File under "things that make you go "Hmmmmmmm"", I guess...... |
|
#6
|
|||
|
|||
Re: ? re strings as character arrays functionQuote:
Quote:
I now that some standards of "c" it is allowed. You should be able to check your compiler documentation to see what to change to flag it as an error. If you need help with that just post what compiler and operating system you are using and someone may be able to suggest the solution to flag that as an error. |
|
#7
|
|||
|
|||
Re: ? re strings as character arrays functionQuote:
|
|
#8
|
||||
|
||||
Re: ? re strings as character arrays functionQuote:
If you create the array via a variable, by definition it is dynamic. IOW the array must be created after the program starts execution because the value of the variable isn't know until after execution starts. The space for the array therefore comes from free memory. __________________
Age is unimportant -- except in cheese |
|
#9
|
|||
|
|||
Re: ? re strings as character arrays functionQuote:
http://msdn2.microsoft.com/en-US/lib...2e(VS.80).aspx Quote:
http://www.open-std.org/jtc1/sc22/wg...wp/html/oct97/ |
|
#10
|
|||
|
|||
Re: ? re strings as character arrays functionQuote:
Current versions of GNU C compilers support most of the features of the so-called "C99" Standard (ISO/IEC 9899:1999), including Variable Length Arrays (VLAs). GNU C++ compilers also allow Variable Length Arrays, even though they are not included in the C++ standard (ISO/IEC 14882:2003). The Borland and Microsoft compilers to which I have access do not support VLAs (or much of anything else beyond the older "C89" standard, and in fact, not all of those features). I have seen no evidence that either company has plans to extend C compiler features to the additional ones in the C99 standards. Borland has re-invented itself yet again (this week), and Microsoft is, well, Microsoft. If you have a GNU C compiler, you can compile programs with VLAs (under the conditions set forth in the Standard--- See Footnote). You could try the following: CPP / C++ / C Code:
If you just execute "gcc z.c" it compiles and works (for me, at least with cygwin/gcc on Windows XP and on Linux with gcc version 3.4.x or 4.x). If you try "gcc z.c -Wall -pedantic", however, you will see warning messages, something like: Code:
However if I execute "gcc z.c -Wall -pedantic --std=c99" it's happy again. (No warnings, and it still works.) For C++, you can try the following: CPP / C++ / C Code:
Now, "g++ z.cpp" gives a working executable with no compiler messages, but if you execute "g++ z.cpp -Wall -pedantic", you will get messages, something like Code:
This time: no executable; it's really an error. My recommendation (especially for programmers-in-training) is usually to stick with the C89 standard for C programs (if it ain't in K&R2: fuggedaboudit) and for the current C++ standard for those programs. (But that's just me --- I'm funny that way.) In my opinion, for C++ programs, the availability of STL vectors and strings obviates the need for VLAs in many, many, many cases. C programmers can use malloc() and free(), the way that we have always done. Regards, Dave Footnote --- from ISO/IEC 9899:1999 Paragraph 6.7.5.2 "All declarations of variably modified (VM) types have to be at either block scope or function prototype scope. Array objects declared with the static or extern storage-class specifier cannot have a variable length array (VLA) type. However, an object declared with the static storageclass specifier can have a VM type (that is, a pointer to a VLA type). Finally, all identifiers declared with a VM type have to be ordinary identifiers and cannot, therefore, be members of structures or unions." That's clear enough for me. Last edited by davekw7x : 25-Apr-2006 at 17:36.
|
Recent GIDBlog
Toyota - 2008 September Promotion by Nihal
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Include] Doubly-linked List | dsmith | C Programming Language | 6 | 14-Apr-2006 13:12 |
| [Tutorial] Function Pointers | aaroncohn | C++ Forum | 4 | 17-Feb-2006 11:33 |
| [GIM] gim.h | dsmith | C Programming Language | 0 | 18-Jan-2005 08:48 |
| Revising Script style ?????? | pepee | MySQL / PHP Forum | 4 | 14-Apr-2004 04:59 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The