Remove Element From Array

Say I define the following array :

char* alertBufferHeader[50];

Then I add ten elements to it :

for(int i = 0; i < 10; i++) {

alertBufferHeader[i] = 'a';

}

Then I remove three elements at random e.g. 3,6 and 7 by setting them to zero leaving 7 non zero elements.

If I'm correct? Then this array has a size of 50 elements with 7 non zero elements.

How would on the fly would I move all the elements preside each other to leave no gaps in the array?

I understand in C I must define how big my array is going to be however when using functions such as sizeof() to get array size and then using that to get number of elements it returns all elements and not just the ones that actually have a character assigned to them. How would I return all elements that have data assigned to them?

char* alertBufferHeader[50];

Thats an array of pointers.

alertBufferHeader[i] = 'a';

'a' is not a pointer.

If you want an array of 50 char, then write

char alertBufferHeader[50] ;

As Nick gammon said, you probably don't really want an array of 50 pointers.

You could also write

char* alertBufferHead = new char[50] ;

which will allocate an array of 50 char also, but you would have to delete it later.

How would on the fly would I move all the elements preside each other to leave no gaps in the array?

char buf[50] ;
for ( int i=0 ; i<10 ; i++ ) buf[i]='a' ;

//  Note !   Right now,  the contents of buf[10] to buf[49]  are unknown,  and could be anything !

buf[3]='\0' ;
buf[6]='\0' ;
buf[7]='\0' ;

char newbuf[47] ;

int i=0 ;
int j=0 ;

while ( i<50 && j<47 )
{
    if ( buf[i] != 0 )
    {
        newbuf[j]=buf[i] ;
        j++ ;
    }
    i++  ;
}

I understand in C I must define how big my array is going to be however when using functions such as sizeof() to get array size and then using that to get number of elements it returns all elements and not just the ones that actually have a character assigned to them. How would I return all elements that have data assigned to them?

You need to be cautious using sizeof( ) to determine array size, it only works when the size is known at compile time. And, if you know the size in advance, you might as well write something like this

#define ARRAYSIZE 50
char buf[ARRAYSIZE] ;

for ( int i=0 ; i<ARRAYSIZE ; i++ )
{
    // do something
}

I would almost always write something like this instead of sizeof(buf). I usually only use sizeof when I need to know how many bytes in an int. 2 or 4 ?

If you wanted to know how many elements of your array have some value other than 0 in them:

char buf[50] ;
for ( int i=0 ; i<10 ; i++ ) buf[i]='a' ;

//  Note !   Right now,  the contents of buf[10] to buf[49]  are unknown,  and could be anything !

buf[3]='\0' ;
buf[6]='\0' ;
buf[7]='\0' ;

int count=0 ;
for ( int i=0 ; i<50 ; i++ )
{
    if ( buf[i] != 0 ) count++ ;
}
//  Note !    Here,  count could be anything,  because you don't know what was in buf[10] to buf[49] !


//   This is how you can allocate a char array with a size ( count ),  which is not known at compile-time.
//   count might be 47,  or indeed any number between 7 and 47, depending on what random garbage is in //    buf[]  .   

char* newbuf = new char[count] ;

//    This array newbuf  must be deleted by explicit code  "  delete[] newbuf ; "  later.

int i=0 ;
int j=0 ;

while ( i<50 && j<47 )
{
    if ( buf[i] != 0 )
    {
        newbuf[j]=buf[i] ;
        j++ ;
    }
    i++  ;
}