c - Adding a size_t variable to a pointer -


i want add size_t type pointer. this:

void function(size_t sizea,size_t sizeb){     void *pointer;     pointer=malloc(sizea);     pointer=pointer+sizeb; } 

in hipothetic case not end in segfault, question is: can this? add type size_t pointer? , resulting address in address 'size'?

can [add size_t pointer]?

yes, can, provided cast void pointer other type:

pointer = ((char*)pointer) + sizeb; 

the type of pointer determines how pointer advanced. if cast char*, each unit of sizeb corresponds 1 byte; if cast int*, each unit of sizeb corresponds many bytes takes store int on system, , on.

however, must ensure sizeb scaled size of pointer cast less or equal sizea, otherwise resultant pointer invalid. if want make pointer can dereferenced, scaled sizeb must strictly less sizea.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -