How to store data from pointer returned by strtok into struct without it being lost?

时光怂恿深爱的人放手 提交于 2021-02-08 10:25:56

问题


I have a malloced string that I needed to parse. I did all of the parsing using strtok(). Strtok has returned a pointer to me and if I printf using that pointer the correct part of the parsed string gets printed. Afterwards I will be freeing the malloced string that the pointer returned by strtok was pointing too.

How do I store what the pointer was pointing at into a struct such that the value remains in the struct variable even after the main string has been freed.

String: Tommy-1234567

My strtok return pointer:

char *studentName= strtok(String1,"-");
char *studentNo= strtok(NULL,"-");

My struct:

typedef struct Student{
    char *name;  //Want name to be stored here even after string is freed
    int  *studentNumber;  //Want no. to be stored here even after string is freed
}Student;

回答1:


In this example about the use of strdup, the program makes copies of the data obtained from strtok. The original string can then be freeed

The code posted has a mistake trying to assign a char* pointer to int*. In C, a textual number is not automatically converted to int.

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

typedef struct Student{
    char *name;
    int  studentNumber;
} Student;

int main(void)
{
    char *string;
    char *tok;
    Student acolyte;

    // set up the input, like this to show use of `strdup` and `free`
    string = strdup("Tommy-1234567");       // allocate memory for source string
    if (string == NULL)                     // check it worked
        return 1;                           // failure

    // student name
    tok = strtok(string, "-");              // isolate first token
    if (tok == NULL)                        // check it worked
        return 1;                           // failure
    acolyte.name = strdup(tok);             // allocated mem for substring and copy
    if (acolyte.name == NULL)               // check it worked
        return 1;                           // failure

    // student number
    tok = strtok(NULL, "-");                // isolate next token
    if (tok == NULL)                        // check it worked
        return 1;                           // failure
    if (sscanf(tok, "%d", &acolyte.studentNumber) != 1)     // extract int
        return 1;                           // failure

    free(string);                           // can now get rid of source data

    // show result
    printf("Name: %s\n", acolyte.name);
    printf("Number: %d\n", acolyte.studentNumber);
    free(acolyte.name);                     // free the memory in struct
    return 0;
}

Program output:

Name: Tommy
Number: 1234567



回答2:


Allocate More Memory

Since you are freeing the string that you parsed, see man(3) strdup

char *newstring = strdup(oldstring);



来源:https://stackoverflow.com/questions/34956110/how-to-store-data-from-pointer-returned-by-strtok-into-struct-without-it-being-l

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!