-2

i've a char **array like this one : {"first", "and", "second", "and", "third"} and i'd like to free(array[3]) and put the pointer to array[1] inside. (because it's the same string). i tried but the pointer wasn't modified. Can you help me pls, thanks.

antocad
  • 3
  • 1

1 Answers1

0

You can't free a pointer that wasn't allocated dynamically with malloc(). Just reassign the pointer without using free:

array[3] = array[1];

This won't likely save any memory, because string literals are allocated statically. And some implementations will merge equal string literals, see Addresses of two pointers are same, so it's effectively done the assignment that you want.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • ok thanks, finally my code was correct, just i was doing printf("%p",&array[i]) instead of printf("%p",array[i]) – antocad Oct 03 '17 at 19:39