I am working on a code with an user interface in C and I have a problem with char pointers. Another person of my team made an algoritm with something like this:
typedef struct
int index;
char * buffer;
//...another fields
} ui_cfg;
int ui_Function (ui_cfg *cfg)
{
char localBuffer[100];
cfg->buffer = localBuffer;
printf("\n[1]cfg->buffer %p", cfg->buffer);
printf("\n[2]localBuffer %p", localBuffer);
Adapter(cfg->buffer);
printf("\n[3]localBuffer %p", localBuffer);
printf("\n[4]cfg->buffer %p", cfg->buffer);
//...irrelevant code
}
This is a very, very simplificated version of the code, but it has the essential parts. In the real code, Adapter is called with a function pointer in a secundary function. Adapter function does a thing like this:
int Adapter (char * buffer)
{
int idx;
idx = sprintf (buffer, "Test string");
return idx;
}
My problem is the next one. When I run the code, it throws Segmentation Fault
in the printf("\n[4]cfg->buffer %p", cfg->buffer);
instruction. However, the other printf works fine and shows the same memory direction. I don't understand why the code crashes at this point. What could be the problem?
Details: This code compiles with arm-brcm-linux-gnueabi-gcc and mipsel-linux-uclibc-gcc on two different architectures. On ARM, this codes works fine and cfg-buffer contains "Test string". On MIPSEL, the code throw segmentation fault.