0

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;
5
  • 2
    How about strdup(studentName)?
    – Baldrick
    Jan 22, 2016 at 21:19
  • 1
    By using strdup. You don't free the (string) pointer returned by strtok: it points to a fragmented part of the string you passed to it. The only thing you free, is the original string pointer (if it was malloced) or the fragment pointers (if they were strduped). Jan 22, 2016 at 21:22
  • @WeatherVane im sorry if I explained badly, but yes, I was reffering to freeing the malloced string.
    – Brandon
    Jan 22, 2016 at 21:32
  • Sorry if I explained it badly. strdup will allocate more memory for, and copy the fragment of the parsed string.You can then free the original string. Later, when you have finished with the student struct, you can free the pointer that was stored in student.name. Jan 22, 2016 at 21:37
  • It's not useful to have a pointer for student.studentNumber. Since the data is int that's more complicated than just storing the number. Jan 22, 2016 at 21:40

2 Answers 2

0

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
4
  • Will i have to free all the pointers I strdup just like I would free any other pointer I malloc?
    – Brandon
    Jan 22, 2016 at 23:53
  • Yes, because strdup calls malloc. Jan 22, 2016 at 23:56
  • Sorry I have one more question regarding this. If I have another free function, where I want to free all the strdups that I did. Is there a way to free all the strdups related to that struct. Or do I literally have to do free(tok)...free(acolyte.studentNumber) one by one.
    – Brandon
    Jan 23, 2016 at 0:09
  • You have to free the pointers returned by strdup one by one. But you must not try to free the pointers returned by strtok. They just point to parts of the original string (which is itself freed), they are not allocated memory. That is why you use strdup, because the tok pointers (and the original string too) will become invalid. In fact, you can see that tok gets overwritten by the next use of strtok. Jan 23, 2016 at 0:29
0

Allocate More Memory

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

char *newstring = strdup(oldstring);

1
  • Will i have to free all the pointers I strdup just like I would free any other pointer I malloc?
    – Brandon
    Jan 22, 2016 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.