I have got a problem with atoi function. From definition it converts string to intiger and as an argument it takes const char*. int atoi (const char *nPtr). If so why this code is wrong
When I use valgrind I receive:
I need to use atoi in such situation in larger project therefore I will apprecciate any kind of help.
Thanks in advance.
Martin
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char *data;
int i,r;
data=malloc(10);
for (i=0;i<10;i++)
data[i]='1';
printf("%s\n",data);
return 0;
r=atoi(data);
}
Code:
==8663== Invalid read of size 1
==8663== at 0x1B9413F8: (within /lib/tls/libc-2.3.5.so)
==8663== by 0x1B9410CE: __strtol_internal (in /lib/tls/libc-2.3.5.so)
==8663== by 0x1B93E6E5: atoi (in /lib/tls/libc-2.3.5.so)
==8663== by 0x804843B: main (test.c:11)
==8663== Address 0x1BA48032 is 0 bytes after a block of size 10 alloc'd
==8663== at 0x1B8FF896: malloc (vg_replace_malloc.c:149)
==8663== by 0x804840D: main (test.c:8)
Thanks in advance.
Martin