0

So I am trying to write my own macro version of a memory copy implementation, yet for some reason it's exiting with non zero status for some reason.

#include <stdio.h>
#include <stdlib.h>

#define BTools_WRITE(dest, src, n) for (size_t i = 0;i<n;i++) { \
                *(unsigned char*)(dest++) = *(unsigned char*)(src++); \
}

int main(void) {
  char* f = "Hello sir!";
  char* u = "4444";
  void* fptr = f;
  void* uptr = u;
  int g = 4;
  BTools_WRITE(fptr, uptr, g);
  printf("%s\n", f);
  return 0;
}

I have tried changing the for loop to while loop, or putting a do-while loop around the whole macro, it always returns with non-zero status. How can I fix this? Can this be done with just a macro?

Josh Weinstein
  • 2,788
  • 2
  • 21
  • 38

1 Answers1

2

You're trying to write to the const data of your program. Try this;

char f[] = "Hello sir!";
cleblanc
  • 3,678
  • 1
  • 13
  • 16