Array length: Difference between revisions

Content deleted Content added
A change from int array to string array
Line 581: Line 581:
</p>
</p>
====Solution with struct====
====Solution with struct====
<lang C>#define _CRT_SECURE_NO_WARNINGS // turn off panic warnings
<lang C>/*******************************************************************************
#define _CRT_NONSTDC_NO_WARNINGS // enable old-gold POSIX names in MSVS

A good (?) solution for the size of an array. A little more is needed for
polymorphic arrays - just use casting and void* instead int.
Obviously, this solution is written in (modern) C, but it would be better
to use C++ (classes, operators redefinitions, templates). Actually in C++
are out-of-box abstract data types (ADT) for this task (see also STL).

*******************************************************************************/


#include <stdarg.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <assert.h>



struct IntegerArray
struct StringArray
{
{
size_t size;
size_t sizeOfArray;
int* elements;
size_t numberOfElements;
char** elements;
};
};
typedef struct StringArray* StringArray;


StringArray StringArray_new(size_t size)
typedef struct IntegerArray* IntegerArray;

IntegerArray IntegerArray_new(size_t size)
{
{
IntegerArray this = calloc(1, sizeof(struct IntegerArray));
StringArray this = calloc(1, sizeof(struct StringArray));
if (this)
if (this)
{
{
this->elements = calloc(size, sizeof(int));
this->elements = calloc(size, sizeof(int));
if (this->elements)
if (this->elements)
this->size = size;
this->sizeOfArray = size;
else
else
{
{
Line 620: Line 616:
}
}


IntegerArray_delete(IntegerArray* ptr_to_this)
void StringArray_delete(StringArray* ptr_to_this)
{
{
assert(ptr_to_this != NULL);
assert(ptr_to_this != NULL);
IntegerArray this = (*ptr_to_this);
StringArray this = (*ptr_to_this);
if (this)
if (this)
{
{
for (size_t i = 0; i < this->sizeOfArray; i++)
free(this->elements[i]);
free(this->elements);
free(this->elements);
free(this);
free(this);
this = NULL;
this = NULL;
}
}
}

void StringArray_add(StringArray this, ...)
{
char* s;
va_list args;
va_start(args, this);
while (this->numberOfElements < this->sizeOfArray && (s = va_arg(args, char*)))
this->elements[this->numberOfElements++] = strdup(s);
va_end(args);
}
}


Line 635: Line 643:
int main(int argc, char* argv[])
int main(int argc, char* argv[])
{
{
StringArray a = StringArray_new(10);
// An easy way to allocate an integer array.
StringArray_add(a, "apple", "orage", NULL);
//
IntegerArray a = IntegerArray_new(10);


assert(a != NULL);
printf(
"There are %d elements in an array with a capacity of %d elements:\n\n",
a->numberOfElements, a->sizeOfArray);


for (size_t i = 0; i < a->numberOfElements; i++)
// An example how to use this array.
printf(" the element %d is the string \"%s\"\n", i, a->elements[i]);
//
for (int i = 0; i < a->size; i++)
a->elements[i] = i * i; // an example expression
for (int i = 0; i < a->size; i++)
printf("element %d is %d\n", i, a->elements[i]);
printf("\nthe array size is %d\n", a->size);


StringArray_delete(&a);
// A safe way to delete array a.
//
IntegerArray_delete(&a);


return EXIT_SUCCESS;
return EXIT_SUCCESS;
}</lang>
}</lang>
{{output}}
{{output}}
<pre>There are 2 elements in an array with a capacity of 10 elements:
<pre>element 0 is 0
element 1 is 1
element 2 is 4
element 3 is 9
element 4 is 16
element 5 is 25
element 6 is 36
element 7 is 49
element 8 is 64
element 9 is 81


the array size is 10
the element 0 is the string "apple"
the element 1 is the string "orage"</pre>
</pre>


====An example why sizeof(A)/sizeof(E) may be a bad idea in C====
====An example why sizeof(A)/sizeof(E) may be a bad idea in C====