• C

how to define a string array with big size in c language

hi,
i want to know how to define a string array with big size like 20,000 characters
 in c language

thanks
LVL 1
hintcoAsked:
Who is Participating?
 
ozoCommented:
char array[20001];
0
 
ozoCommented:
A string is a pointer to a null terminated array of chars, so do you mean:
a char array with 20,001 characters?
an array of 20,000 strings?
an array of 20 strings with 1,001 characters each?
an array of 1,000 strings with 21 characters each?
something else?
0
 
hintcoAuthor Commented:
I mean a char array with 20,001 characters?

thanks
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
OrcbighterCommented:
Don't forget to initialise it after you have created the thing
const unsigned long MaxArray = 20001;

char charArray[MaxArray];

memset( charArray, 0, MaxArray );

// or if you are in a Windows environment
SecureZeroMemory( charArray, MaxArray );

Open in new window

This will fill the array with zeros and ensure there is no garbage data in the array you created.
0
 
ozoCommented:
char charArray[20001] = "";
suffices to initialize charArray as an empty string
0
 
sarabandeCommented:
to add to above:

char charArray[20001] = { '\0' };

Open in new window


would initialize all 20,001 characters with a zero character.

char * pCharArray = (char*)malloc(20001);
/* use array */
...
free(pCharArray);       /* after use */

Open in new window


would create an array dynamically on the heap.

by using calloc instead of malloc the meory was zero'ed.

Sara
0
 
ozoCommented:
technically, char * pCharArray does not define an array with big size
 = (char*)calloc(20001); assigns a string with big size
(or potentially big size, since zeroing the first element makes strlen(pCharArray)==0)
0
 
OrcbighterCommented:
technically, calloc takes two arguments, number of elements and size:
char* pArray = (char*)calloc( 1, 2000 );
And instead of using strlen, I would use the sizeof operator
simple test program to illustrate the point
const unsigned int arrayLength = 2000;

int _tmain(int argc, _TCHAR* argv[])
{
    char* pArray1 = (char*)malloc( arrayLength );
    char* pArray2 = (char*)calloc(1, arrayLength );
    wchar_t* pArray3 = (wchar_t*)calloc(1, arrayLength );

    size_t len1 = strlen( pArray1); // 2016 bytes - error because it was not initialised
    size_t len2 = strlen( pArray2); // 0 bytes
    size_t len3 = wcslen( pArray3); // 0 bytes

    int len1b = sizeof( *pArray1 );  // 1 byte ascii * length
    int len2b = sizeof( *pArray2 );  // 1 byte ascii * length = 2000 (correct)
    int len3b = sizeof( *pArray3 );  // 2bytes - unicode (correct)

    int len1c = sizeof( *pArray1 ) * arrayLength;  // 1 bytes- ascii * length = 2000 (correct)
    int len2c = sizeof( *pArray2 ) * arrayLength;  // 1 byte - ascii * length = 2000 (correct)
    int len3c = sizeof( *pArray3 ) * arrayLength;  // 2 bytes - unicode * length = 4000 (correct)

    cout << "length of (char) pArray1 (strlen) = " << len1 << endl;    // 2016
    cout << "length of (char) pArray2 (strlen) = " << len2 << endl;    // 0
    cout << "length of (char) pArray3 (wcslen) = " << len3 << endl;    // 0

    cout << "length of (char) pArray1 (sizeof) = " << len1b << endl;   // 1
    cout << "length of (char) pArray2 (sizeof) = " << len2b << endl;   // 1
    cout << "length of (char) pArray3 (sizeof) = " << len3b << endl;   // 2

    cout << "length of (char) pArray1 array = " << len1c << endl;      // 2000
    cout << "length of (char) pArray2 array = " << len2c << endl;      // 2000
    cout << "length of (char) pArray3 array = " << len3c << endl;      // 4000
    return 0;
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.