I am sending a string from one program to another: The received program sees the correct string and it is stored in string buffer. The number used in the token and string statement in the string Related case code is shown below. The input string is: which first minimizes the program: The output of the second program is as follows: Can someone help me to understand what's happening here? I could not properly use the What's happening here Undefined behavior: You can copy To fix this problem, you must either have Finally, there is a thread-safe option for using number lastname firstname middlei ID GPA .
tok = strtok (arg, ""); Printf ("First token% s \ n", Tasks); Memcpy (ptr-> student.lname, Token, 10); Tok = strtok (NULL, ""); Mempi (pt-> student.fam, tokay, 10); Tok = strtok (NULL, ""); PTR- & gt; Student.initial = * torque; Tok = strtok (NULL, ""); Sscanf (torque, "% lu", & amp; ptr-> student.SID); Tok = strtok (NULL, ""); Sscanf (Toe, "% F", and PTR-> Students GPA); // Some other indicator and location initial ... printf ("lu% s% s% c% f \ n", ptr-> student. Sid, pt-> student.lumb, pt-> student.fam, PTR-> Student Initial, PTR-> Students GPA);
5 lastnamereallylong, firstnamereallylong, X, 1234.4.0
5 The first name of the second name is X 1234 4.0
first line lastnamere 1234 lastnamereXfirstnamer firstname 4.000000
memcopy but I did not think that the sequence of basic information would change.
memcpy Which is the first ten characters of tokens, but when tokens are longer than ten characters, this means that the string is not going to be redundant. Passing this string to
printf ("% s", ...) with undefined behavior.
ptr- & gt; Student.lname [9] = '\ 0' If you want to be with fixed-length strings, or use
strdup to allow variable-length strings.
strtok - you can use it.
No comments:
Post a Comment