delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2004/04/05/10:56:56

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
Subject: RE: function question
Date: Mon, 5 Apr 2004 10:55:01 -0400
Message-ID: <ACAAE6BD7520AB4CB2744D1D1BAD3A2B7094CF@jupiter.insideai.com>
X-MS-Has-Attach:
X-MS-TNEF-Correlator:
Thread-Topic: function question
Thread-Index: AcQa+xperpgYFBNFSeyhW3a4MuSZSAAGdTrg
From: "John Bond" <Jbond AT ai-logix DOT com>
To: <djgpp AT delorie DOT com>
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 <string.h>
#include <stdlib.h>
#include <stdio.h>

/*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<len; i++)
   {
      if(i < len/2)
	  {
	     *p = *string_in_temp;  /*copy one character from string_in to szString1*/
		 p++;                   /*move insertion pointer to next char*/
		 string_in_temp++;      /*move char pointer to next char*/
	  }
      else if(i == len/2)
	  {
	     *p = 0;                /*mark the end of the string in szString1*/
		 p = szString2;         /*now have p point to start of szString2*/
	     *p = *string_in_temp;  /*copy one character from string_in to szString2*/
		 p++;                   /*move insertion pointer to next char*/
		 string_in_temp++;      /*move char pointer to next char*/
	  }
	  else
	  {
	     *p = *string_in_temp;  /*copy one character from string_in to szString2*/
		 p++;                   /*move insertion pointer to next char*/
		 string_in_temp++;      /*move char pointer to next char*/
	  }
   }
   *p = 0; /*mark end of szString2*/

   return len;
}

int main()
{
   char szMainString[] = "This line to be cut in half...";
   char szMainString2[] = "This line is even longer and requires more space...";

   szString1 = NULL;
   szString2 = NULL;

   iCutInHalf(szMainString);

   printf("%s\n", szMainString);
   printf("%s\n", szString1);
   printf("%s\n", szString2);

   if(szString1 != NULL)
   {
      free(szString1);
	  szString1 = NULL;
   }
   if(szString2 != NULL)
   {
      free(szString2);
      szString2 = NULL;
   }


   iCutInHalf(szMainString2);

   printf("%s\n", szMainString2);
   printf("%s\n", szString1);
   printf("%s\n", szString2);

   if(szString1 != NULL)
      free(szString1);
   if(szString2 != NULL)
      free(szString2);

   return 0;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019