Studying memcpy. yes it works but it would be nice if we could just keep a reference to the subpart of the array we are interested in with no copy at all.
char a[] = "hello";
char b[20];
memcpy(b,a,strlen(a)-1)
Could we do the same with no copy at all, just pointer arithmetic ?
printf("my array is %s\n",a..a+2)
for example ?
The point is not only printf but any reference at any place in the code by a pointer for the beginning and the end. a..a+2 (think socket, function arguments..)
To me copying seems a bit weird for a slice since it should be a reference to the parent array.
Using fwrite from stdio.h you can print any part of your C-style string as you please as fwrite takes a pointer to any character in the string and expects you to supply a size instead of using a null-byte termination.
The declaration of fwrite is as follows:
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );
See also http://www.cplusplus.com/reference/cstdio/fwrite/
So you can write
const char* a = "foobarfoobar";
fwrite(a + 3, 6, 1, stdout);
which will print the substring "barfoo" to stdout.
There are also several other functions in the standard library that take a pointer and a size for raw memory management that can be used if you want to handle substrings without having to copy the original string just to insert a null-byte at the end of the substring. Like fwrite these functions allow you to handle any arbitrary binary data but are therefore also suitable for character string manipulation and handling.
As you edited your question, here's a more general explanation for passing around parts of an array:
Functions that accept arrays containing arbitrary data always take two parameters. One pointer to the array and one for the size of the array. Of course they can also take additional arguments, but these two are the minimum.
void some_function(const void *ptr, size_t size);
if you have an array const char[] foo = "some data"
you can use pointer arithmetic to determine the start of a sub part. foo+n
will generate a pointer to the n+1th element of the array. You can therefore pass foo + 2
as ptr
to some_function
making ptr
point to the 3rd element of the foo array. In this case this is the memory address that contains the letter 'm' of the string "some data". Starting from that position you can tell the function the size of the array through the size
argument. If you choose size to be 5 you can call some_function
as some_function(foo + 2, 5);
which will make some_function
effectively see "me da".
To compare two subarrays you can use the function memcmp
from string.h (http://www.cplusplus.com/reference/cstring/memcmp/).
This function also accepts just like some_function
a pointer and a size argument. Additionally it accepts a second pointer to the memory location to compare to.
const char[] foo = "some data";
const char[] bar = "same database";
memcmp(foo + 2, bar + 2, 7); // Will return 0 indicating equality as foo+2 -> foo+2+7 and bar+2 -> bar+2+7 both result in the substring "me data".