Click here to Skip to main content
14,825,178 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi guys,
A MFC coder want to learn some basic about charecter array intialisation and deletion of element.Take following examples campare with MFC (there is CString so no need of memory allocation or de allocation but same needed in c.)
(dont have std::string interface :-()

Example 1:-
To construct string we us following code in MFC.
C++
CString constructString;
constructString = "";
constructString = "ABC";
constructString = constructString + "PQR";
constructString = constructString + "LMN";

whatever size of string we have this will work.

for C i used following code
C++
#define DEFAULT_ARRAY_SIZE			20000
char* constructString  = new char[DEFAULT_ARRAY_SIZE];
strcpy(constructString ,"");
strcat(constructString ,"ABC");
strcat(constructString ,"PQR");
strcat(constructString ,"LMN");

Problem :-
1)Code will work fine till my char* constructString size is less than 20000 but when it exceed i dont have solution,how to resize my array so it will take more charecters.
2)I intialize char* constructString with 20000 but when my string is very small of size 10 then my remaining 18990 charecters are wasted or not i dont know,will this effect my executable perfomance.If yes then how to delete my remaining dummy charecters.

Example 2:-
To read content from file we use following code in MFC.
C++
CStdioFile ReadFile;
ReadFile.Open("Sample.txt",CFile::typeText|CFile::Read);
CString CurrentString;
CStringArray WholeFile;
while(ReadFile.ReadString(CurrentString))
{
    WholeFile.Add(CurrentString);
}

Whitever size of File it will work fine.

For C i use following code
C++
#define MAX_FILE_SIZE				65534
FILE *ptr_file;
const char* list[MAX_FILE_SIZE];
wchar_t CurrentString[1000];
ptr_file =fopen("Sample.txt","rb");
int __index = 0;
while(fgetws (CurrentString , 1000 , ptr_file) != NULL)
{
	char* errorDes;
	errorDes = new char[1000];
	wcstombs(errorDes, CurrentString, 1000);
	list[__index] = errorDes;
	__index++;
}

Problem :-
1)Same as above if my one line charecters exceed 1000 then more than 1000 charecters are not consider and vise versa.
2)If my file size exceed 65534 then char* list array will not fill properly and vise versa.

________________________________________
Please provide me any link,block of code,suggestion that help me to solve all problem in pure c.

Thanks.
Posted
Updated 7-May-13 2:12am
v4

1 solution

All your problems are solved using dynamic memory allocation: you shouldn't allocate a static array but a bit more complex data structure (emulating CString or std::string, behaviour). e.g.
C
typedef struct _MyString
{
  char * str;
  int len;
  int capacity;
} MyString;


Then you should provide some functions for manipulating such a string, for instance:
C
int mystring_init(MyString * pms, const char * src);
int mystring_cat(MyString *pms, const char * src);
int mystring_destroy(MyString * pms);
// ...and so on....



A rough mystring_init implementation could be:
C
// error checking omitted for sake of brevity
int mystring_init(MyString * pms, const char * src)
{
  pms->len = strlen(src+1);
  pms->capacity = pms->len;
  pms->str = (char*) malloc(pms->capacity+1);
  strcpy(pms->str, src);
  return 0;
}

in mystring_cat implementation you need to reallocate the internal memory, that's accomplished using the realloc[^] function:
C
// error checking omitted for sake of brevity
int mystring_cat(MyString * pms, const char * src)
{
  pms->len += strlen(src+1);
  pms->capacity = pms->len;
  pms->str = (char*) realloc(pms->str, pms->capacity+1);
  strcat(pms->str, src);
  return 0;
}


Please note the above are (untested) naive implementation, written only to give you an idea.
   
Comments
Coder Block 8-May-13 1:53am
   
Can you tell me one thing,
If i create one character array of size like,
char* TempArray[10];
how do i intialise it???
CPallini 8-May-13 3:31am
   
char* TempArray[10]; declares an array of character pointers.
If you want to declare an array of characters then you have to write: char array[10];.
You may initialize it as any other array, assigning values to the array items, e.g.
a[9] ='\0'; for (n=0; n<9; n++) array[n]='A';
You may also use library functions like strncpy.
Finally initializer lists are allowed, e.g.
char array[10]={'0','1', '2', '3', '4', '5', '6', '7', '8', '9'};
or
char array[10]="012345678"; // please note the missing '9'
Coder Block 8-May-13 3:43am
   
I got you point as i can create charecter array using char array[10];
but problem is that i want to create array of character array means just like CStringArray in MFC.For Example,
if there 2 string like,
"ABCD",
"PQR";
I just want one array that contain both ABCD and POR ,
Something like follow:-
char* Temp[2];
Temp[0] = "ABCD";
Temp[2] "PQR";
.
.
.
//now code change and i need more element in Temp then,
//Resize temp to 3 and
Temp[3] = "LMN";
CPallini 8-May-13 3:54am
   
So you need an array of strings. You may resize it using realloc, e.g.
Temp = realloc( Temp, sizeof(char *) * 3);
(make sure to check the result of the realloc call).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900