• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

C Integer Array Extraction

 
sashwat kumar padhy
Greenhorn
Posts: 20
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an array of integers.Let's assume int a[5]={1,2,1,2,2};.I want to divide this array into two arrays by extracting some elements of array "a[5]" into some different arrays like "b[2]" and "c[3]",such that int b[2] will contain {1,2} and int c[3] will contain {1,2,2}. How can it be possible using C programming?
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sashwat kumar padhy wrote:I have an array of integers.Let's assume int a[5]={1,2,1,2,2};.I want to divide this array into two arrays by extracting some elements of array "a[5]" into some different arrays like "b[2]" and "c[3]",such that int b[2] will contain {1,2} and int c[3] will contain {1,2,2}. How can it be possible using C programming?


Depends on what you mean by extraction. If you mean two new arrays, which contains a copy of the elements of the original array, then allocate the two new arrays, use a loop, and copy the elements.

However -- and this is one of the cool things about C and pointer arithmetic -- if you mean for the two new arrays to be backed by the original array, then you can do it with pointers. In C, a pointer behaves just like an array (or more correctly, an array behaves like a pointer). So, the "b" array is just an int pointer, pointing to the zeroth element of the "a" array. And the "c" array is just an int pointer, pointing to the second element of the "a" array.

Henry
 
sashwat kumar padhy
Greenhorn
Posts: 20
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
sashwat kumar padhy wrote:I have an array of integers.Let's assume int a[5]={1,2,1,2,2};.I want to divide this array into two arrays by extracting some elements of array "a[5]" into some different arrays like "b[2]" and "c[3]",such that int b[2] will contain {1,2} and int c[3] will contain {1,2,2}. How can it be possible using C programming?


Depends on what you mean by extraction. If you mean two new arrays, which contains a copy of the elements of the original array, then allocate the two new arrays, use a loop, and copy the elements.

However -- and this is one of the cool things about C and pointer arithmetic -- if you mean for the two new arrays to be backed by the original array, then you can do it with pointers. In C, a pointer behaves just like an array (or more correctly, an array behaves like a pointer). So, the "b" array is just an int pointer, pointing to the zeroth element of the "a" array. And the "c" array is just an int pointer, pointing to the second element of the "a" array.

Henry

Thank you..I wanted to make separate arrays which are independent of the array "a".Did it using memcpy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic