0
int main(void)
{
   const char *word = "Hello";

   int n = strlen(word);
   char copy[n];
   for (int i = 0; i < n; i++)
   {
     copy[i] = toupper(word[i]);
   }

   printf("%s\n", copy);
}

The code above compiles and prints what I expect however, running valgrind points some errors that I don't understand and some help would be greatly appreciated to understand the issue that it points out and how to fix it. Thank you.

==2188== Conditional jump or move depends on uninitialised value(s)
==2188==    at 0x483EF58: strlen (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck- 
            amd64-linux.so)
==2188==    by 0x4A60E94: __vfprintf_internal (vfprintf-internal.c:1688)
==2188==    by 0x4A62021: buffered_vfprintf (vfprintf-internal.c:2377)
==2188==    by 0x4A5EEA3: __vfprintf_internal (vfprintf-internal.c:1346)
==2188==    by 0x4A49EBE: printf (printf.c:33)
==2188==    by 0x4011EC: main (test.c:18)
==2188==  Uninitialised value was created by a stack allocation
==2188==    at 0x401170: main (test.c:11)
==2188== 
Noobster
  • 11
  • 2

0 Answers0