Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unhandled exception while using strcpy() -- Urgent 1

Status
Not open for further replies.

kathyayini

Programmer
Aug 16, 2002
52
IN
I am attaching the code :
char* inullable[20];
char* temp_inullable="1,2,3"

int nLoop=0;
char cstemp_str[20]="a";
strcpy(cstemp_str,"");
int icoma_trap=0;
nLoop=0;
while(*temp_inullable !='\0')
{
if(*temp_inullable !=',')
{
strncat( cstemp_str, temp_inullable, 1);
temp_inullable++;
}
else
{
strcpy(inullable[nLoop],cstemp_str);
strcpy(cstemp_str,"");
temp_inullable++;
nLoop++;
}
}

program is giving the err "Unhandled exception/ Access violation" while executing "strcpy(inullable[nLoop],cstemp_str);" line what could be the problem

waiting for the reply
 
It's not clear what you're trying to achieve here(???) - I'm assuming you want to examine the string [tt]temp_inullable[/tt] - if the character is not a comma, append it to the string [tt]cstemp_str[/tt] - if it is a comma, add it to [tt]inullable[/tt].

Also, why do this?:

[tt]char cstemp_str[20]="a";
strcpy(cstemp_str,"");[/tt]

when you can just:

[tt]char cstemp_str[20]="";[/tt]


Here's some modified code which will extract the commas...


[tt]char inullable[20]="";
char temp_inullable[20]="1,2,3"

int nLoop=0;
char cstemp_str[20]="";
int icoma_trap=0; // <- what's this??????

while(nLoop<strlen(temp_inullable))
{
if(temp_inullable[nLoop] !=',')
{
cstemp_str[strlen(cstemp_str)]=temp_inullable[nLoop];
cstemp_str[strlen(cstemp_str)+1]=0;
}
else
{
inullable[strlen(inullable)]=temp_inullable[nLoop];
inullable[strlen(inullable)+1]=0;
}
nLoop++;
}[/tt]

I noticed that you were using [tt]temp_inullable++[/tt] - if you wish to delete the first character and move the rest down (thereby making the string shorter) use this:

[tt]memmove(str,str+1,strlen(str));[/tt]

:)
tellis.gif

[sup]programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.[/sup]​
 
strcpy(inullable[nLoop],cstemp_str);
tryes to copy string to uninitialized pointer. It must be defined as
char inullable[20][30];
or something like that.
 
Hi kathiyayini,
Yes what mingis told was right. U are trying to access unallocated area in memory. thats why it is crashing for the variable char* inullable[20];

I have pasted ur code with modification. only MODIFICATION is memory allocation.

char* inullable[20];
char* temp_inullable=&quot;1,2,3&quot;;
int nLoop=0;
char cstemp_str[20]=&quot;a&quot;;
strcpy(cstemp_str,&quot;&quot;);
int icoma_trap=0;
nLoop=0;


for(int it=0;it<20;it++)
inullable[it] = new char[20];


while(*temp_inullable !='\0')
{
if(*temp_inullable !=',')
{
strncat( cstemp_str, temp_inullable, 1);
temp_inullable++;
}
else
{
strcpy(inullable[nLoop],cstemp_str);
strcpy(cstemp_str,&quot;&quot;);
temp_inullable++;
nLoop++;
}
}

I hope this will help u

S.Senthil Kumar.
 
Perhaps you are trying to seperate commas from numbers.
I think whatever you are using you are allocating no memory space to a pointer.'20' which you declared perhaps your are mistaking it as an array while it is a simple memory space buffer.Simple increasing one more dimension will ease your task.Both of these gentlemen are right.

char inullable[3][20];//Modification here
char* temp_inullable=&quot;1,2,3,&quot;;

int nLoop=0;
char cstemp_str[20]=&quot;a&quot;;
strcpy(cstemp_str,&quot;&quot;);
int icoma_trap=0;
nLoop=0;
while(*temp_inullable !='\0')
{
if(*temp_inullable !=',')
{
strncat( cstemp_str, temp_inullable, 1);
temp_inullable++;
}
else
{
strcpy(inullable[nLoop],cstemp_str);
strcpy(cstemp_str,&quot;&quot;);
temp_inullable++;
nLoop++;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top