2

I have three Pointers:

int *x,*y, *Temp;

And then I have done

x = (int *)malloc(sizeof(int)*m);
y = (int *)malloc(sizeof(int)*n);
Temp = (int *)malloc(sizeof(int)*(m+n));

where m and n are certain values.

Next, I have entered values into Temp.

  for(i=0; i < m+n; i++) {
  scanf("%d", Temp+i);
  }

I want half of Temp in x and the other half in y. How do I do this?

for(i=0; i < m; i++) {
  x[i] = Temp[i];
}

The code above to copy the contents is not working!

Also, how do I print the values?

7
  • 3
    Usual disclaimer: In C you should not cast the result of malloc (or any function returning void *). Commented Oct 13, 2015 at 5:29
  • 3
    As for your problem, can you please be more specific? Just saying "this is not working" doesn't really tell us much, how is it not working? Do you get build errors? Run-time errors? Unexpected results? Something else? And please try to create a Minimal, Complete, and Verifiable Example and show us. Commented Oct 13, 2015 at 5:32
  • Why did you add 2 to the size of Temp? Commented Oct 13, 2015 at 5:33
  • It is compiling and running but getting a garbage value in the place. Commented Oct 13, 2015 at 5:34
  • you may use calloc which initializes the allocated memory or use memset to set the memory locations to NULL. Commented Oct 13, 2015 at 5:37

4 Answers 4

6

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]);
}
Sign up to request clarification or add additional context in comments.

6 Comments

So in this, first m values are copied to X from Temp. Then I need to increment 1 place and assign the rest (everything but the last) to y. how do i go about that?
@Vidya Just use the example that I gave
@PCLuddite Please see it once running . I think it throws garbage .
memcpy() is copying only half the array correctly. The other half is 0s.
@M.M That was residual from an edit. It originally said "using memcpy and printf in a loop". I moved printf to a different section, but neglected to remove the "in a loop" part. I fixed the answer.
|
3

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.

1 Comment

Its look clearer to me for sure. Beauty lies on the eyes of beholder after all.
3

Use memcpy.It will copy desired numbre of bytes .

What I was doing is already mentioned in this answer of PC Luddite

1 Comment

for memcpy(), how many bytes should I consider? I need to put half the value in x and the other half in y
0

It doesn't really look like you need to allocate all that memory, nor does it seem like you need to copy it. Consider using this alternative approach:

int* xy = malloc(sizeof(*xy) * (m+n));
int* x = xy;
int* y = xy + n;
...
free(xy);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.