In your 'p' method, you're assigning pointer b to be pointer a, or, in otherwords, you're making b point to what a points to. Any changes to b will cause changes to a since they both wind up pointing to the same block of memory.
Use memcpy to copy blocks of memory. It would look something like this:
#include <string.h>
#include <stdlib.h>
void p(int *a){
int *b = (int*)malloc(sizeof(int)*4);
memcpy(b, a, sizeof(int)*4);
//make changes to b.
b[0] = 6;
b[1] = 7;
b[2] = 8;
b[3] = 9;
}
int main(int argc, char **argv)
{
int *a = (int*)malloc(sizeof(int)*4);
// add values to a
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
p(a);
return 0;
}
Answer from Brandon Haston on Stack OverflowIn your 'p' method, you're assigning pointer b to be pointer a, or, in otherwords, you're making b point to what a points to. Any changes to b will cause changes to a since they both wind up pointing to the same block of memory.
Use memcpy to copy blocks of memory. It would look something like this:
#include <string.h>
#include <stdlib.h>
void p(int *a){
int *b = (int*)malloc(sizeof(int)*4);
memcpy(b, a, sizeof(int)*4);
//make changes to b.
b[0] = 6;
b[1] = 7;
b[2] = 8;
b[3] = 9;
}
int main(int argc, char **argv)
{
int *a = (int*)malloc(sizeof(int)*4);
// add values to a
a[0] = 1;
a[1] = 2;
a[2] = 3;
a[3] = 4;
p(a);
return 0;
}
Just assigning the pointer means b is pointing to the same chunk of memory as a and the memory you just allocated "for b" has leaked. It's allocated but you can't free it any more.
To copy the array you need to well, copy it.
Easy way is lookup the various memcpy methods, or just do it the longer way
for (int i = 0; i < 4; i++) {
b[i] = a[i];
}
You need to know about "shallow copy" and "deep copy" - since you have an array of int*, what I put above is a shallow copy. If you need to copy the contents that each int* points to, you'll need something more complex again.
c - Copying array to array using pointer - Stack Overflow
Copying arrays using pointers? - C++ Forum
How to copy contents of one array to another using pointers in C? - Stack Overflow
c - Copying Array Using Pointers - Stack Overflow
Videos
Using memcpy is probably the easiest way to accomplish what you want:
memcpy(x, Temp, m * sizeof*Temp);
memcpy(y, &Temp[m], n * sizeof*Temp);
To print the values, just use printf:
puts("Values in x:");
for(int i = 0; i < m; ++i) {
printf("%d\n", x[i]);
}
puts("Values in y:");
for(int i = 0; i < n; ++i) {
printf("%d\n", y[i]);
}
You can use as follows to print the values. I just use M in place of m for better clarity.
for(i=0; i < M; i++) {
printf("VAL: %d\n",x[i]);
}
Similarly you can also print another array y.
we simply use the strcpy function to copy the array into the pointer.
char str[] = "Hello World";
char *result = (char *)malloc(strlen(str)+1);
strcpy(result,str);
In your first snippet you modify the pointer in the loop, so after the loop it will no longer point to the same location returned by the malloc call.
Look at it this way, after the call to malloc the pointer and the memory look like this:
result | v +---+---+---+---+---+---+---+---+---+---+---+---+ | | | | | | | | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+
After the first iteration of the loop it will look like this:
result
|
v
+---+---+---+---+---+---+---+---+---+---+---+---+
| H | | | | | | | | | | | |
+---+---+---+---+---+---+---+---+---+---+---+---+
And after the loop is done it will look like this
result
|
v
+---+---+---+---+---+---+---+---+---+---+---+----+
| H | e | l | l | o | | W | o | r | l | d | \0 |
+---+---+---+---+---+---+---+---+---+---+---+----+
The copying is made, but the pointer no longer points to the original position.
[Note: Before the copying, the memory allocated by malloc will not be "empty" or initialized in any way, I just show it like that here for simplicity's sake.]