Question

I'm doing a project for school and I must use char * intead of string. I need to modify some parts of the char * but every time I try I have memory issues.

The name has this format: "1.1.2" and what I want to do is return a c* that contains "1.1.3" so I need to get the last char from the name and make a +1.

This is part of the code:

struct ve
{
     char * name;
};

char * modificar(ve *v)
{
    char *c= new char(strlen(v->name)+1);
    //I don't know what to do here to get the results I want
    return c; 
}
Was it helpful?

Solution

A very basic approach to do exactly what you asked is outlined below:

char *c = strdup(v->name);
c[strlen(c)-1]++;
return c;

However, it has problems for things like 1.1.9, which requires quite a bit more complicated logic.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top