kp11
kp11

Reputation: 2125

Can mem copy be implemented without any pointer arithmetic in C?

Implementation of my_memcpy (memory copy from source to destination based on size) uses a lot of pointer arithmetic, which is a very common method of implementation.

Issue: Based on certain standards, my code is not supposed to use pointer arithmetic, but can use arrays.

Is there any way we can implement that function without using any pointer arithmetic?

I want the below logic to be implemented without the use of pointers:

while (u32_length >= SIZE)
{
    *u32p_dst = *u32p_src;  
     u32p_dst++;
     u32p_src++;
     u32_length -= SIZE;            
}
....
....    

Upvotes: 0

Views: 100

Answers (2)

suitianshi
suitianshi

Reputation: 3340

actually you can treat the pointer and an array element the same here

to improve performance, normally using a pointer is faster(but in most cases this is not the bottleneck)

Upvotes: 0

Zac Wrangler
Zac Wrangler

Reputation: 1445

what about the following,

unsigned i = 0;
while(u32_length >= SIZE){
   u32p_dst[i] = u32p_src[i];
   ++ i; 
   u32_length -= SIZE;
}

Upvotes: 2

Related Questions