Hi All,
I am trying to get the length of an char pointer but the app is getting crashed by giving error - Access Violation. I am not getting why this is happening. The piece of code is given below -
CODE
Code:
int length = 0;
char** strArr = (char**)malloc(sizeof (char * )*(4));
temp=(char*)malloc(sizeof(char*)*100);
tempChar="HELLO";
strArr[0] = (char*)malloc(sizeof(char*)*(strlen(tempChar)+1));
strcpy(strArr[0],tempChar);
tempChar="HELLO1";
strArr[1] = (char*)malloc(sizeof(char*)*(strlen(tempChar)+1));
strcpy(strArr[1],tempChar);
tempChar="HELLO2";
strArr[2] = (char*)malloc(sizeof(char*)*(strlen(tempChar)+1));
strcpy(strArr[2],tempChar);
tempChar="HELLO3";
strArr[3] = (char*)malloc(sizeof(char*)*(strlen(tempChar)+1));
strcpy(strArr[3],tempChar);
length = get2DArrayLength(strArr);
int get2DArrayLength(char** charArray)
{
int len=0;
char **ptr;
for(ptr=charArray;ptr!='\0';*ptr++)
{
print("0..%s",*ptr);
if(*ptr==NULL)
break;
if(strlen(*ptr)!=0)
{
len+=strlen(*ptr);
}
print("1..%s",*ptr);
}
return len;
}
OUTPUT ::
0..HELLO
1..HELLO
0..HELLO1
1..HELLO1
0..HELLO2
1..HELLO2
0..HELLO3
1..HELLO3
segmentation fault
Please check the code and tell me what I am doing wrong in this..
thanks.