My code is here:
char* kropecka(char* tab)
{
int a=0,b=0;
char* zwr;
zwr=(char*)malloc(30*sizeof(char));
for(a;strlen(tab);a++)
{
if(tab[a]!='.')
{
if(isupper(tab[a]))
zwr[b]=tolower(tab[a]);
if(islower(tab[a]))
zwr[b]=toupper(tab[a]);
b++;
}
}
zwr[b]='\0';
return zwr;
}
There is no errors, warnings or something like this. But program crashed when I give him some string:
--------------------------- Microsoft Visual C++ Debug Library --------------------------- Debug Assertion Failed!
Program: ...s\Visual Studio 2010\Projects\C_homework\Debug\C_homework.exe File: f:\dd\vctools\crt_bld\self_x86\crt\src\isctype.c Line: 56
Expression: (unsigned)(c + 1) <= 256
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
--------------------------- Abort Retry Ignore
Compilator: Visual Studio 2010 Included libary: stdio.h, string.h, ctype.h, stdlib.h (for system() function in main() ).
The exit test for your loop is wrong.
strlen(tab)
will always return either false or true (depending ontab
). This means that you continue writing tozwr
beyond its allocated length. The effects of this are undefined but its not surprising that it eventually crashes.You can fix this by changing the loop to
Another possible cause of error is that
zwr
is hard-coded as 30 bytes. This clearly isn't sufficient for all possible values oftab
so you could change the code toThis will run forever:
I think you meant:
Or better (because
strlen
is O(n)):From the C standard:
Emphasis mine.
MSDN's description of
toupper()
hints at this as well:isascii():
Check the callstack - hopefully it will take you to the root of the problem.
This method looks like one that checks for character value bounds...
isctype.c -
In simple terms it seems that you are overflowing on a variable i.e. Unsigned Char can take a maximum value of 255. If you add 1 to an existing value of 255 - you overflow on it.
Hope it helps.