J. Doe Newmann J. Doe Newmann - 2 months ago 14
C Question

Pointer of substring from pointer of string

I'm quite new to C and struggle with the following.


Consider a string (pointer to an array of characters), containing an arbitrary amount of unsigned bytes, in our case, let it be
programming
. Write a function, which returns a pointer to a new location in memory (but not allocated with
malloc
), containing the substring of arbitrary but fixed size, at an arbitrary offset - in our case, let it be
gramm
.


I tried with the following:

unsigned char* newptr(unsigned char* p, int offset, int size) {
unsigned char r[size];
memcpy(r, p + offset, size + 1);
return r;
}


but it errors with
Segmentation fault
. Otherwise

unsigned char* newptr = oldptr + offset;


works but it doesn't solve the problem with the fixed size - I don't want the rest of chars belonging to the pointer.

Thanks in advance and sorry if the question was asked before, I just couldn't find anything of help.

Answer

Some possible solutions, all them are not very good:

Using global buffer (using global variable is bad practice and if you call the function again it rewrites the buffer, buffer has fixed size and it must be larger then your size):

unsigned char r[100000];

unsigned char* newptr(unsigned char* p, int offset, int size) {
    memcpy(r, p + offset, size);
    r[size] = '\0'
    return r;
 }

Using static local buffer (has mostly the same problems as solution with global buffer):

unsigned char* newptr(unsigned char* p, int offset, int size) {
    static unsigned char r[100000];        
    memcpy(r, p + offset, size);
    r[size] = '\0'
    return r;
}

Using memory of input variable as buffer (the problem is then *p changes after the function call)

unsigned char* newptr(unsigned char* p, int offset, int size) {
    p[offset + size] = '\0'
    return p + offset;
}

Passing allocated buffer into function:

unsigned char* newptr(unsigned char* p, int offset, int size, unsigned int* r) {
    memcpy(r, p + offset, size);
    r[size] = '\0'
    return r;
}