Access violation when using strtok in C (using Visual Studio 2019)

0

Hi StackOverflow community, I come to you in a time of great need.

My question concerns the use of strtok() in C, using the "Visual Studio" IDE (2019)

Here is my code :

#include<stdio.h>
#include<stdlib.h>
#include<string.h>



int main()
{

    char num1 = "", num2 = "", num3 = "", num4 = "", num5 = "", num6 = "", num7 ="", num8 = "", num9 = "", num10 = "", num11 = "", num12 = "", num13= "", num14 = "", num15 = "", num16 = "";
    int int1, int2, int3, int4, int5, int6, int7, int8, int9, int10, int11, int12, int13, int14, int15, int16;
    FILE* file = NULL;
    fopen_s(&file, "sauv.txt", "r");

    char chaine[101] = "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16";
    char* point = NULL;
    char* buffer = NULL;

    char rend[100] = { 0 };

    num1 = strtok(chaine, " ");
    printf("num = %s", num1);
    num2 = strtok(NULL, " ");
    printf("num = %s", num2);




    return 0;
}

It's just a simple test program to isolate this problem that I had in a bigger program.

Here is the error I get : error

(Exception thrown at 0x7A3B1F4C (ucrtbased.dll) in TEST.exe: 0xC0000005: Access violation reading location 0xFFFFFF8C.)

From what I've seen, It's linked to my program trying to access a destination (like the address of an array) that is out of reach, or that isn't accessible. Apparently, it's caused by not initiating an array/pointer or by trying to access the [i+1] value of an array defined to only be [i] long. I don't see how that's happening in my code.

I've also seen that it's because strtok() returns a value that is not changeable or something like that.

I'm trying to cut the string "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16" in "1" "2" "3" "4" ... Here i've only done the code to get "1" and "2".

If someone could explain to me what the source of the problem is that would be great!

(I know it probably comes from the way I created my strings or something like that but I've been trying to solve this on my own for days now and it still confuses me)

Thanks for the answers!

c
string
visual-studio
strtok
asked on Stack Overflow May 23, 2020 by Adam Le Roux

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0