c - memcpy() by reference and no copy? -
reference
study memcpy Yes it works, but it would be good if we just Referring to the array's sub-reference, which we are not interested in any copy.
char a [] = "hello"; Four b [20]; Memcpy (b, a, strlen (a) -1)
question
Can not we make any copies on it, just pointing arithmetic?
printf ("My array is% s \ n", a..a + 2)
For example?
The point is not only printf but any reference to any place in the code by an indicator for beginning and end A. .. A + 2 (seems socket, function argument)
< P> I think a bit strange for a piece to copy because it should be the reference of the original array.
You can print any part of your c-string string using fwrite from stdio.h Yes, because please please take a pointer in any of the characters in the string and hope that you supply the size instead of using an empty-byte end.
The announcement of the present is as follows:
a = "foboraphobar"; filitt (A3 , 6, 1, standout); size_t fwrite (const zero *
which will print the substrings of "barfoo" in stdout.
There are several other functions in the standard library that are a pointer and crude Take a shape for memory management, it can be used if you subtoted an empty-byte without sub-compatibility without copying the original string to the substrings These functions, like frames, allow you to handle any arbitrary binary data, but hence the characters are also suitable for string manipulation and handling.
Like If you edit your question, here is a more general explanation for crossing parts of an array:
We accept tasks that arbitrarily arise. They take two parameters, one for the array and one for the size of the array. Of course they can also take additional logic, but these are both minimum.
(Static zero * ptr, size_t size);
If you have an array const char [] foo = "some data"
you can use pointer arithmetic to determine the beginning of a sub section Are there. foo + n will generate an indicator of element n + 1 element of the array
. So you can send the third element of the foo array to foo + 2
as ptr
as some_function
and send the ptr
point Are there. In this case this is the memory address in which the string of "some data" is the letter 'm', starting with that condition you can tell the size of the function through the size
argument if you want to get 5 You can call some_function
as some_function (foo + 2, 5);
which will "see me" effectively "some_function .
To compare two subarrays, you can use the function memcmp
from string (h). This function also accepts an indicator and size argument such as some_function
. It also accepts a second pointer in the memory space to compare it.
const char [] foo = "some data"; Const char [] Bar = "same database"; Memcmp (foo + 2, times + 2, 7); // will return 0 signal of equality foo + 2 - & gt; Fu + 2 + 7 and Bar + 2 - & gt; Both times + 2 + 7 results as a result of "I have data". I
Comments
Post a Comment