-1

I've looked through the site to find a way to get the number of elements of an array with data in it. All I can find is people using sizeof, but that isn't an option for me due to the teachers limitations on what we can access. I need a way of doing it in a loop, preferably the same loop where I'm reading data from.

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

#define SIZE 10

int readData();

int
main(void)
{
double data[SIZE];
int size;

readData(double data[], &size);

return 0;

}

int readData(double data[], int* size)
{
FILE* fp;
fp = fopen("data.txt", "r");
int i;

for (i = 0; i < SIZE; i++)
{
    if( fscanf(fp, "%lf", &data[i]) < 1)
    break;
    if(data[i] < 0)
        printf("Negative data at %d\n", i);
}
size = i;

printf("The array is: ");
for (i = 0; i < size; i++)
{
    printf("%f ", data[i]);
}
fclose(fp);

printf("\n\n");

return;
}
user3857017
  • 265
  • 4
  • 14

2 Answers2

2
int arr[] = {1, 2, 3, 4, 5};
size_t size = ( (char *)(&arr + 1) - (char *) (&arr) ) / sizeof arr[0];
printf("%zu\n", size);

live code here

For reuse you can write a macro as below

#define ARR_SZ(ARR) ( ( (char *)(&ARR + 1) - (char *) (&ARR) ) / sizeof ARR[0] )

EDIT
If you are reading from a file at run time, you can keep a track of succesful read which will be the size of your array.

size_t size = 0;
while( fread(arr + size, sizeof arr[0], 1, fp) ) ++size;

size will contain the count of succesfully read items.

EDIT2
Here is a live code example of returning the size and below is the modified code.

int readData(double data[], int size)   /* Change return type */
{
    FILE* fp;
    fp = fopen("data.txt", "r");
    int i;

    for (i = 0; i < size; i++)
    {
        if( fscanf(fp, "%lf", &data[i]) < 1) break; /* Added */
        if(data[i] < 0)
            printf("Negative data at %d\n", i);
    }
    size = i; /* Added */

    printf("The array is: ");
    for (i = 0; i < size; i++)
    {
        printf("%f ", data[i]);
    }
    fclose(fp);
    printf("\n"); /* Added */

    return size; /* Added */
}

In above example you can increase or decrease the number of inputs.

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • Edited code above for completeness as well, maybe it will make more sense in full – user3857017 Jul 20 '14 at 04:00
  • I adapted this code to mine as best I could, and for some reason when I try to test the value of size in main (via printf("The size of size is %d", size);) it says 1989992264. – user3857017 Jul 20 '14 at 04:13
  • I've managed to get it to something more logical, it's 49 now but I feel like that's just the ASCII code for the character s in int form. – user3857017 Jul 20 '14 at 04:30
  • thanks for all of your help, you were very helpful and very thorough! – user3857017 Jul 20 '14 at 05:04
2

Using info from this question:

int size = (&data)[1] - data;

The [1] treats &data as an array. Arrays are indexed by starting with the location of index 0, adding the index number multiplied by the size of an element, and dereferencing the result.
Because the element in this case is the original array, (&data)[1] returns the address of the index after the end of the array. Subtracting the address of the array returns the number of elements.
(see link for slightly less awkwardly worded explanation).

Community
  • 1
  • 1
McLovin
  • 3,554
  • 1
  • 14
  • 15