X-Authentication-Warning: delorie.com: mail set sender to djgpp-bounces using -f X-MimeOLE: Produced By Microsoft Exchange V6.0.6249.0 content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Subject: RE: function question Date: Mon, 5 Apr 2004 10:55:01 -0400 Message-ID: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: function question Thread-Index: AcQa+xperpgYFBNFSeyhW3a4MuSZSAAGdTrg From: "John Bond" To: Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id i35EuFqV006806 Reply-To: djgpp AT delorie DOT com Bill, You wrote: >I've seen this in either k&r2 or a C++ book. Can't remember which. > An array say char array1[]={}; >It's initializing an array to how a yet indetermined number of elements. C >is a small language. But it the beginning can be very confusing. At least to >me. Most newbies find pointers to be confusing and I'm still not quite sure >where & always comes into play. I find among good C programmers that some >that are good and some that are greater. Depends on their experience I >guess. C does not provide for unspecified length arrays, but instead provides for pointers and heap memory management. If you pursue these topics, 1) proper use of pointers and 2) heap management (malloc() and free()) you will find what you need in the C language. Continue to gain your own experience, it is worth it. Consider the following (sorry it is not short, but it did compile ;): #include #include #include /*these two strings to hold two halves of a string of unknown length*/ char *szString1 = NULL; /*global: pointer to nowhere right now*/ char *szString2 = NULL; /*global: pointer to nowhere right now*/ int iCutInHalf(char *string_in) { int i, len; char *p; char *string_in_temp = string_in; if( (szString1 != NULL) || (szString2 != NULL) ) { /*warn developer that a memory leak may occur, and exit*/ printf("Error: free memory allocated in pString1 and/or pString2 prior to calling iCutInHalf()\n"); exit(1); } len = strlen(string_in); p = szString1 = malloc(len/2 + 1); szString2 = malloc(len/2 + 1); if( (szString1 == NULL) || (szString2 == NULL) ) { printf("Error: unable to allocate memory for pString1 and/or pString2\n"); if(szString1 != NULL) free(szString1); if(szString2 != NULL) free(szString2); exit(1); } string_in_temp = string_in; for(i=0; i