Using a pointer to intersect all elements of an INTEGER array

Is there a way to intersect all elements in an integer array using a pointer (similar to using a pointer to intersect string elements). I know that an integer array is not NULL terminated, so when I try to traverse an array using a pointer, it overflows. I added NULL as the last element of the array, and it worked fine.

int array[7]={1,12,41,45,58,68,NULL};
int *i;
for(i=array;*i;i++)
    printf("%d ",*i);

But what if one of the elements in the array is 0, which will behave like NULL.Is is there any other way that will implement the pointer when crossing all the elements in the integer array?

+2
source share
8 answers

, C- , - 256 , " ". - C- .

, . - , " , ". , , , - , .

++ , C, . , - , ++ (, vector, string list), begin() end(), . ,

for (iterator itr = begin; itr != end; ++itr)
    /* ... visit *itr here ... */

, - , . - , . , .

+2

, , . , , , -1, , . C- ; NULL , , .

- , , , .

- :

// Note that you don't have to specify the length of the array.
int array[] = {1,12,41,45,58,68};
// Let the compiler count the number of elements for us.
int arraySize = sizeof(array)/sizeof(int);
// or int arraySize = sizeof(array)/sizeof(array[0]);

int main()
{
    int* i;
    for(i = array; i != array + arraySize; i++)
        printf("%d ",*i);
}

:

int arrayBegin[] = {1,12,41,45,58,68};
int* arrayEnd = arrayBegin + sizeof(arrayBegin)/sizeof(arrayBegin[0]);

int main()
{
    int* i;
    for(i = arrayBegin; i != arrayEnd; i++)
        printf("%d ",*i);
}

, , , . , ! ( , .)

, , :

  • , ,
  • , , , .
+5

: - / . . , .

++ STL .

C ,

typedef struct t_int_array 
{
    size_t length;
    int data[1]; /* note the 1 (one) */
} int_array;

,

int_array * new_int_array(size_t length)
{
    int_array * array;
    /* we're allocating the size of basic t_int_array 
      (which already contains space for one int)
      and additional space for length-1 ints */
    array = malloc( sizeof(t_int_array) + sizeof(int) * (length - 1) );
    if(!array)
        return 0;
    array->length = length;
    return array;
}

int_array * concat_int_arrays(int_array const * const A, int_array const * const B);
int_array * int_array_push_back(int_array const * const A, int const value);
/* and so on */

t_int_array , ( malloc), , .

+2

, STL, :

int array[6]={1,12,41,45,58,68};
for (int i = 0; i < sizeof(array) / sizeof(array[0]); ++i)
{ }

, ​​:

template<size_t len> void func(int (&array)[len])
{
    for (int i = 0; i < len; ++i) { }
}

int array[6]={1,12,41,45,58,68};
func(array);

0 - , , :

const int END_OF_ARRAY = 0x80000000;
int array[8]={0,1,12,41,45,58,68,END_OF_ARRAY};
for (int i = 0; array[i] != END_OF_ARRAY; ++i)
{ }

(, ), . , (, BSTR).

0

( ) NULL ; NUL ('\0') C, C NUL , ( ).

NULL , ( ++, int, , C ). , , , NULL. , , , , .

, , :

static const int SENTINEL_VALUE = -1 ;
int array[7] = { 1, 12, 41, 45, 58, 68, SENTINEL_VALUE } ;
int* i ;

for( i = array; *i != SENTINEL_VALUE; i++ )
{
    printf( "%d ", *i ) ;
}

, ( ), ( sizeof()).

0

, : C, . (1) . ? / ( 32- ) 0 0xffffff. "", .

, (1) (1) , . , int- , , , NULL ( ): `


int my_int_array[10];  // maximum of 10 integers in my_int_array[], which must be static
int member_count = 0;  // varies from 0 to 10, always holds number of ints in my_int_array[]
int *
first_greater_than ( int val ) {
  int i;
  int *p;
  for ( i = 0, p = my_int_array; i < member_count; ++i, ++p ) {
    if ( *p > val ) {
      return p;
    }
  }
  return NULL;
}

i, my_int_array [], .. 9, p my_int_array [10] :


int my_int_array[10];  // maximum of 10 integers in my_int_array[], which must be static
int member_count = 0;  // varies from 0 to 10, always holds number of ints in my_int_array[]
int *
first_greater_than ( int val ) {
#define MAX_COUNT sizeof(my_int_array)/sizeof(int)
  int  i;
  int* p;
  for ( i = 0, p = my_int_array; i < member_count && i < MAX_COUNT; ++i, ++p ) {
    if ( *p > val ) {
      return p;
    }
  }
  return NULL;
}

HTH, , , .

- Pete

  • , ,
0

ANSI C , :

int array[]={1,12,41,45,58,68}, *i=array;
size_t numelems = sizeof array/sizeof*array;
while( numelems-- )
    printf("%d ",*i++);
0

Another way is to manage an array of pointers to int:

#include <stdlib.h>
#include <stdio.h>

#define MAX_ELEMENTS 10

int main() {
 int * array[MAX_ELEMENTS];
 int ** i;
 int k;

 // initialize MAX_ELEMENTS,1 matrix
 for (k=0;k<MAX_ELEMENTS;k++) {
  array[k] = malloc(sizeof(int*));
  // last element of array will be NULL pointer
  if (k==MAX_ELEMENTS-1)
    array[k] = NULL;
  else
    array[k][0] = k;
 }

 // now loop until you get NULL pointer
 for (i=array;*i;i++) {
  printf("value %i\n",**i);
 }

 // free memory
 for (k=0;k<MAX_ELEMENTS;k++) {
  free(array[k]);
 }

 return 0;
}

Thus, the condition of the cycle is completely independent of the values ​​of integers. But ... for this you must use a 2D array (matrix) instead of the usual 1D array. Hope this helps.

0
source

Source: https://habr.com/ru/post/1794900/


All Articles