0

Does anyone know how one can create a subarray by extracting a part of an existing array. Is there something like subarray[] =existingArray[0-3]?

I need this to compare the first 4 bytes of my block in recover.c with the first 4 bytes of jpeg arrays.

1

No need to extract the first four elements to compare them. You could do something like

if (array[0] == val0 && array[1] == val1 && array[2] == val2 && array[3] == val3)
{
    // do something
}

However, if you need to extract the first n elements of an int array and put them in their own separate array, you may have something like

void extractElements(int srcArray[], int subArray[], int n)
{
    for (int i = 0; i < n; i++)
    {
        subArray[i] = srcArray[i];
    }
}

The previous function takes 3 arguments, the array from which you wanna extract the first n elements (i.e., srcArray), the array to which you wanna extract these elements (i.e., subArray) and the number of elements to be extracted (i.e., n).

Notice that this function doesn't check for some errors like whether the number of elements to be extracted is more than the number of elements in srcArray or whether subArray is not big enough to hold the n elements.

The function of the standard libarary, namely memcpy (declared in string.h), can be used for this purpose too. Execute

man memcpy

in the terminal for more information!

Hope that helps!

1
  • I did think of the first solution but it seemed too long to write as a condition. Wanted something shorter and better looking to keep my code tight. Yep came across memcpy too. I guess I'll try something with that. – user1580 Sep 29 '14 at 4:38
1

TLDR : If you are searching for a solution specifically for the pset, then I would suggest Kareem's first answer.


This answer is exactly with reference to the question mentioned in your title..

There are such features in python, but I haven't searched for whether they are available in c99. However, I managed to write something useful in this context.

Here is a function that works on strings, and extracts characters from it, with index in the range [start, end](both inclusive), at each discrete steps. It returns the length(to be considered) for the edited array.

int parts(char* array, int start, int end, int steps)
{
     int i, j;
     char temp [(end-start)/steps + 1];
     for (i = start , j = 0 ; i <= end ; i += steps , ++j)
           temp[j] = array[i];
     for (i = 0 ; i < (end - start)/steps + 1 ; ++i)
           array[i] = temp[i];
     //for ( ; i <= end ; ++i)
     //      array[i] = ' ';
    return (end - start)/steps + 1;
}

Consider char array[] = "abcdefghijklmnopqrstuvwxyz";. When calling this function and printing characters of array from 0 to returned value of the function,

parts(array, 0, 25, 1);
parts(array, 0, 25, 2);
parts(array, 10, 25, 3);

we get output as these respectively.

abcdefghijklmnopqrstuvwxyz
acegikmoqsuwy
knqtwz

A similar implementation for integer arrays looks like this.

int parts(int* array, int start, int end, int steps)
{
    int i, j;
    int temp [end];
    for (i = start , j = 0 ; i <= end ; i += steps , ++j)
        temp[j] = array[i];
    for (i = 0 ; i < j ; ++i)
        array[i] = temp[i];
    //for ( ; j < end ; ++j)
    //  array[j] = 0;
    return (end - start)/steps + 1; 
}

Consider int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};, then function calls and their respective outputs are :

parts(arr, 0, 14, 1);
parts(arr, 0, 14, 2);
parts(arr, 5, 14, 3)

Outputs something like this :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1 3 5 7 9 11 13 15
6 9 12 15

Remember, don't mess with values of start, end, and steps. Also don't experiment with any values such that start > end or steps < 0, the functions are not implemented to support such expectations. Although, feel free to modify the function in any way you want to add these extra features.

Good Luck.


P.S. : Also, the functions partially modify the array passed to them, so it would be good only if the values of that array are accessed for index from 0 to the value returned by those functions. If you want to keep the original array unchanged, then try creating its duplicate and pass it to these functions.

1
  • maybe I should've been specific in my question that I wanted to apply a method using some predefined mechanisms. And in that this route sort of defeats the purpose. But your answer satisfied the rubric of my vaguely put doubt any way so +1 for you! Thanks for the effort! – user1580 Oct 2 '14 at 14:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .