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!

Adam Le Roux
  • 43
  • 1
  • 1
  • 7
  • `num1` is `char`. Change it to `char*` type. – Eraklon May 23 '20 at 16:55
  • The above should not even compile. Are you sure that's the real code? – dxiv May 23 '20 at 17:03
  • Yeah it's the actual code, but is there something else you see in the code that could be a problem ? – Adam Le Roux May 23 '20 at 18:56
  • changing to char* still gets me the same error :( (Exception thrown at 0x7A3B1F4C (ucrtbased.dll) in TEST.exe: 0xC0000005: Access violation reading location 0xFFFFFFE6.) – Adam Le Roux May 23 '20 at 18:57
  • @AdamLeRoux Make a habit to compile with "*treat warnings as errors*" (`/WX`). Fix all those warnings that you now ignore and you will have found your problems. – dxiv May 23 '20 at 19:02
  • that's a good idea on my main code I have like 22 different warnings lol I guess I should fix that :) – Adam Le Roux May 23 '20 at 19:09
  • @AdamLeRoux "*changing to char\**" Make sure you changed *all* of them `char *num1, *num2;`. – dxiv May 23 '20 at 19:11
  • also doesn't char* create a "read-only", not changeable string? which means i wouldn't be able to modify it with strtok() right ? – Adam Le Roux May 23 '20 at 19:17
  • @AdamLeRoux `char *` declares a char pointer. You can initialize it point to a read-only string, see for example [Initializing a char pointer in C. Why considered dangerous?](https://stackoverflow.com/questions/8795576/initializing-a-char-pointer-in-c-why-considered-dangerous). This doesn't apply in your case, however, since the changes effected by `strtok` are inside the `chaine[]` array, which is *not* read-only. – dxiv May 23 '20 at 19:27
  • hmm ok, I won't initialize them then thanks! ( edit: IT SOLVED MY PROBLEM THANK YOU I LOVE YOU !!!) – Adam Le Roux May 23 '20 at 19:35

0 Answers0