0

For whatever reason I am unable to decipher why the compiler is throwing the uninitialized local variable exception based on a few methods I've tried. My current code is as follows.

while (fgets(line, LINE_SIZE, fh) != NULL)
{
    const char s[1] = " ";
    char * token;
    token = strtok(line, s);
    char * tokptr;

    for (int i = 0; i < MAX_NUM; i++)
    {
        nums[i] = *token;
    }

    while (token != NULL)
    {
        strcpy(tokptr, token);
        token = strtok(NULL, s);

    }
}

I'm receiving an error at the line: strcpy(tokptr, token) for an uninitialized local variable 'tokptr' and I'm lost as to why.

I've tried pre-allocating the memory for the variable by using char * tokptr = malloc(200); but have also not had any luck with that.

Justin
  • 125
  • 1
  • 2
  • 12
  • 2
    You do know that the string literal `" "` is actually ***two*** characters? The space, and the ***null-terminator*** that's needed by all string functions to find the end of the string. Initializing a one-character array like that will not include the terminator, leading all string function you pass the array to to go out of bounds. – Some programmer dude Dec 07 '18 at 13:55
  • 2
    As for your problem: Where is `tokptr` pointing? The initialization you show should have solved the problem (with the compiler warning message). – Some programmer dude Dec 07 '18 at 13:56
  • 1
    `char * tokptr = malloc(200);` Should have worked. What problem did you have with that? – dbush Dec 07 '18 at 13:57
  • I'm very new to C and pointers, so my usage on them is very ambiguous. i'm trying to follow the requirements for the methods, and I'm not sure where it's going wrong. Could you explain possibly what it should be pointing to? `tokptr` just needs to store the char string from `token`. `token` is filled just fine by the `strtok(NULL, S)` method. – Justin Dec 07 '18 at 14:00
  • I'm guessing the problem is that, assuming a reasonable level of smartness, the compiler can deduce that the call to `strtok` will read memory assigned to `tokptr`, which has not been initialized. Changing the order of declaration of the local variables could shed light on this. – Tim Randall Dec 07 '18 at 14:01
  • Even if you allocate space for `tokptr`, you don't do anything with it after you've filled it with the contents of `token`, not least of which `free`ing it...so what's the point of it? – Chris Turner Dec 07 '18 at 14:02
  • @dbush I run into a runtime error when I use `char * tokptr = malloc(200);` With the expression: buffer != nullptr – Justin Dec 07 '18 at 14:02
  • What is `nullptr`? Do you compile with a C++ compiler? – Gerhardh Dec 07 '18 at 14:03
  • No, I'm using VS 2015, but I've set everything to use C compiler, so I thought? – Justin Dec 07 '18 at 14:04
  • 1
    "I'm receiving an error at the line: strcpy(tokptr, token) for an uninitialized local variable 'tokptr' and I'm lost as to why." That would be because tokptr is an uninitialized local variable. – Lundin Dec 07 '18 at 14:04
  • @Lundin Thank you for stating the obvious. But clearly, by my question I'm referring to why it is considered uninitialized when I have stated that I've tried pre-allocating the memory and seeing how memory is allocated for it, I am trying to copy the contents of `token` into `tokptr`, then the error is being thrown. – Justin Dec 07 '18 at 14:05
  • What do you want to do with `tokptr` and `strcpy(tokptr, token);` and `nums[i] = *token;`? – duong_dajgja Dec 07 '18 at 14:06
  • 1
    @Justin mallocing space for `tokptr` fixes the warning. If you get a runtime error, you did something else wrong. – dbush Dec 07 '18 at 14:06
  • @Justin Then you need to update your question with the real code, in the form of a [mcve]. As it stands, you just try to copy data into an uninitialized pointer. – Lundin Dec 07 '18 at 14:07
  • 1
    I'm guessing you think that declaring a variable `char* tokptr` allocates memory. It does, for the _pointer itself_. Not for the _pointed-at_ data. See the linked duplicate. – Lundin Dec 07 '18 at 14:09
  • @Justin, if you insist that you get an error when in fact you are initializing variable `tokptr`, then I suggest you update your question to present *that* code as your MCVE, along with whatever warning or error the compiler reports for it. The problem with the code currently presented is exactly what the compiler says: `tokptr` is not initialized. – John Bollinger Dec 07 '18 at 14:09
  • @Lundin I looked at the linked duplicate. It helped me figure out the answer, thank you. I'd not have known to search for that, as I am rather new to C. Sorry for the misunderstanding. – Justin Dec 07 '18 at 14:10

0 Answers0