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?

Vidya
  • 233
  • 3
  • 11
  • 3
    Usual disclaimer: [In C you should not cast the result of `malloc`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) (or any function returning `void *`). – Some programmer dude Oct 13 '15 at 05: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](http://stackoverflow.com/help/mcve) and show us. – Some programmer dude Oct 13 '15 at 05:32
  • Why did you add `2` to the size of `Temp`? – PC Luddite Oct 13 '15 at 05:33
  • It is compiling and running but getting a garbage value in the place. – Vidya Oct 13 '15 at 05:34
  • you may use `calloc` which initializes the allocated memory or use `memset` to set the memory locations to NULL. – Pawan Oct 13 '15 at 05:37
  • The small parts of code you show doesn't tell us the whole story, and the parts you show if put together in a single `main` function should work just fine (for reasonable values of `m` and `n`, and if `` has been included). You really need to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) and show us. – Some programmer dude Oct 13 '15 at 05:42
  • The code you have posted so far is correct, please post a MCVE showing the problem. Also, instead of "is not working", say: (a) what happened, (b) what you expected. – M.M Oct 13 '15 at 05:49

4 Answers4

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]);
}
PC Luddite
  • 5,883
  • 6
  • 23
  • 39
  • 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 Oct 13 '15 at 05:40
  • @Vidya Just use the example that I gave – PC Luddite Oct 13 '15 at 05:46
  • @PCLuddite Please see it once running . I think it throws garbage . – ameyCU Oct 13 '15 at 05:51
  • memcpy() is copying only half the array correctly. The other half is 0s. – Vidya Oct 13 '15 at 05:52
  • @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. – PC Luddite Oct 13 '15 at 05:52
  • @ameyCU @Vidya I didn't multiply the number of bytes by `sizeof*Temp` (this is what I get for not testing it first). The code works now. – PC Luddite Oct 13 '15 at 06:01
3

Use memcpy.It will copy desired numbre of bytes .

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

Community
  • 1
  • 1
ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • for memcpy(), how many bytes should I consider? I need to put half the value in x and the other half in y – Vidya Oct 13 '15 at 05:37
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.

Pawan
  • 1,537
  • 1
  • 15
  • 19
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);
Lundin
  • 195,001
  • 40
  • 254
  • 396