Possible Duplicate:
Pass a buffer by reference to other function
When I call the function ast It saves data on buffer and in bufferlen, but I try to access this pointer from dec and I can print *bufferlen but *buffer it´s impossible.
When I do temp=buffer a segmentation fault appears.And in the debugger I see in buffer “Address 0x7f out of bounds”. How can I print buffer from dec?? Which is the error?
void ast(KOCTET *buffer,KUINT16 *bufferlen){
KOCTET *Bufferencode,*temp;
KUINT16 BufferSize=5000;
KUINT16 WritePos=0;
KUINT16 total_bytes;
Bufferencode=(KOCTET*)malloc(5000*sizeof(KOCTET));
memset(Bufferencode,0,sizeof(Bufferencode));
total_bytes = stream.CopyIntoBuffer( Bufferencode, BufferSize, WritePos);
buffer=Bufferencode;
*bufferlen=total_bytes;
int z=0;
temp=buffer;
while (z<(*bufferlen)){
printf(" %02X",(unsigned char)*temp);
temp++;
z++;
}
printf("\n");
}
void dec()
{
KOCTET *buffer,*temp;
KUINT16 *bufferlen;
ast_process_048(buffer,bufferlen);
int z=0;
temp=buffer;
while (z<(*bufferlen)){
printf(" %02X",(unsigned char)*temp);
temp++;
z++;
}
printf("\n");
}
There are several problems with your code.
astyou access*bufferlenseveral times, without having memory allocated to it before.I suppose you want to hand over the memory allocated for
Bufferencodetobufferfor further use outsideastwith your statementbuffer=Bufferencode;. However, this would require:These are just the first obvious problems – there might be more. Consider the suggestions in the answer to your yesterday’s question and – maybe – learn a bit more about basic C programming and pointer handling at all.