I'm writing some Code that does some basic analysis on data.
The data collected is in an array of structs. It takes the approximate form:
struct page {
int width;
int length;
char name[50];
// etc...
}
struct page pages[100];
I need to write code that finds the smallest width, largest width, smallest length, largest length, etc. So I write code that looks something like this:
int smallestWidth(struct page pages[]){
unsigned int smallest = -1;
(for loop){
if (smallest > pages[i].width) smallest = pages[i].width;
}
return smallest;
}
And then I find that I'm copy-pasting this function and changing tiny details for the other requirements like largest width. And whenever I'm copy-pasting chunks of code, that raises alarm bells for me, and I'm thinking I'm doing it wrong.
But, I'm kind of new to C, so I'm not sure what the right way to approach this is.
How would you write this in C properly (if there is a proper way) that minimizes the amount of code that I'm copy-pasting?
C++ language has overrides and pointer to members that C has not. So the C way would be to use auxilliary functions to extract the correct data and compare them:
int getLength(struct page *page) {
return page->length;
}
int getWidth(struct page *page) {
return page->width;
}
int lesser(int a, int b) {
return a<b;
}
int greater(int a, int b) {
return a>b;
}
typedef int(*extractor)(struct page *p);
typedef int (*comparator)(int a, int b);
int process(struct page * p, int size, extractor ext, comparator cmp) {
// code here the generic part
...
}
int lesserWidth = process(pages, 100, &getWidth, &lesser);
...
But it includes a good deal of boiler plate code, so it may be interesting or not depending on the complexity of the generic part...
I think the following will be classified as a Horrible Hack, however, it satisfies your request. It only works for integers because of the compare.
struct page {
int width;
int length;
// etc...
} pages[100] = {{1,2},{3,4},{5,6}};
int smallestX(struct page pages[], size_t offset)
{
unsigned int smallest = -1, k;
for (int i=0; i<3; i++){
k= *((int *)((char *)(&pages[i])+offset));
if (smallest > k) smallest = k;
}
return smallest;
}
void example(void)
{
printf ("smallest width= %d\n", smallestX(pages, offsetof(struct page, width)));
printf ("smallest length= %d\n", smallestX(pages, offsetof(struct page, length)));
}
Clarification:
It uses the offsetof macro to get the offset of the member from the beginning of the struct. In the function, it now takes the address of the ith element of the array, interprets that as a byte address, adds the offset (which is in bytes), interprets that as a pointer to an int, dereferences that int and uses it in the compare.
Note: it is possible to extend this method to compare any item by providing a compare function as parameter.
I was reading over some of the source code behind pngquant (here)
I got confused when I saw plus-equals seemingly assigning a new value to an array of structs (base += r in the code snippet below):
static void hist_item_sort_range(hist_item base[], unsigned int len, unsigned int sort_start)
{
for(;;) {
const unsigned int l = qsort_partition(base, len), r = l+1;
if (l > 0 && sort_start < l) {
len = l;
}
else if (r < len && sort_start > r) {
base += r; len -= r; sort_start -= r;
}
else break;
}
}
The hist_item definition is given as:
typedef struct {
f_pixel acolor;
float adjusted_weight, // perceptual weight changed to tweak how mediancut selects colors
perceptual_weight; // number of pixels weighted by importance of different areas of the picture
float color_weight; // these two change every time histogram subset is sorted
union {
unsigned int sort_value;
unsigned char likely_colormap_index;
} tmp;
} hist_item;
I apologize ahead of time, because I'm sure to those in the know this must be a really dumb question, but how is plus-equals operating on base, which appears to be an array of structs, and some integer r? It seems to me that this operation should be undefined for the combination of those two types.
I haven't had to write C for almost ten years, and I'm admittedly pretty rusty; however, searching for about thirty minutes only turned up answers to the wrong questions, and any help is appreciated. Thanks!
As explained in What is array decaying?
static void hist_item_sort_range(hist_item base[], unsigned int len, unsigned int sort_start)
becomes
static void hist_item_sort_range(hist_item* base, unsigned int len, unsigned int sort_start)
Where base is a pointer to the first element of the array. Therefore base += r; performs simple pointer arithmetic, i.e.: modifies the pointer to point to an offset of r elements from the start of the array.
Due to the += the original pointer is modified, so any access happens with offset from the now pointed to element.
To use the example from the comment:
After base += 1; accessing the "first" element via &base[0]; yields a pointer to the same element as &base[1]; before the increment
I have a function in C which calculates the mean of an array. Within the same loop, I am creating an array of t values. My current function returns the mean value. How can I modify this to return the t array also?
/* function returning the mean of an array */
double getMean(int arr[], int size) {
int i;
printf("\n");
float mean;
double sum = 0;
float t[size];/* this is static allocation */
for (i = 0; i < size; ++i) {
sum += arr[i];
t[i] = 10.5*(i) / (128.0 - 1.0);
//printf("%f\n",t[i]);
}
mean = sum/size;
return mean;
}
Thoughts:
Do I need to define a struct within the function? Does this work for type scalar and type array? Is there a cleaner way of doing this?
You can return only 1 object in a C function. So, if you can't choose, you'll have to make a structure to return your 2 values, something like :
typedef struct X{
double mean;
double *newArray;
} X;
BUT, in your case, you'll also need to dynamically allocate the t by using malloc otherwise, the returned array will be lost in stack.
Another way, would be to let the caller allocate the new array, and pass it to you as a pointer, this way, you will still return only the mean, and fill the given array with your computed values.
The most common approach for something like this is letting the caller provide storage for the values you want to return. You could just make t another parameter to your function for that:
double getMean(double *t, const int *arr, size_t size) {
double sum = 0;
for (size_t i = 0; i < size; ++i) {
sum += arr[i];
t[i] = 10.5*(i) / (128.0 - 1.0);
}
return sum/size;
}
This snippet also improves on some other aspects:
Don't use float, especially not when you intend to return a double. float has very poor precision
Use size_t for object sizes. While int often works, size_t is guaranteed to hold any possible object size and is the safe choice
Don't mix output in functions calculating something (just a stylistic advice)
Declare variables close to where they are used first (another stylistic advice)
This is somewhat opinionated, but I changed your signature to make it explicit the function is passed pointers to arrays, not arrays. It's impossible to pass an array in C, therefore a parameter with an array type is automatically adjusted to the corresponding pointer type anyways.
As you don't intend to modify what arr points to, make it explicit by adding a const. This helps for example the compiler to catch errors if you accidentally attempt to modify this array.
You would call this code e.g. like this:
int numbers[] = {1, 2, 3, 4, 5};
double foo[5];
double mean = getMean(foo, numbers, 5);
instead of the magic number 5, you could write e.g. sizeof numbers / sizeof *numbers.
Another approach is to dynamically allocate the array with malloc() inside your function, but this requires the caller to free() it later. Which approach is more suitable depends on the rest of your program.
Following the advice suggested by #FelixPalmen is probably the best choice. But, if there is a maximum array size that can be expected, it is also possible to wrap arrays in a struct, without needing dynamic allocation. This allows code to create new structs without the need for deallocation.
A mean_array structure can be created in the get_mean() function, assigned the correct values, and returned to the calling function. The calling function only needs to provide a mean_array structure to receive the returned value.
#include <stdio.h>
#include <assert.h>
#define MAX_ARR 100
struct mean_array {
double mean;
double array[MAX_ARR];
size_t num_elems;
};
struct mean_array get_mean(int arr[], size_t arr_sz);
int main(void)
{
int my_arr[] = { 1, 2, 3, 4, 5 };
struct mean_array result = get_mean(my_arr, sizeof my_arr / sizeof *my_arr);
printf("mean: %f\n", result.mean);
for (size_t i = 0; i < result.num_elems; i++) {
printf("%8.5f", result.array[i]);
}
putchar('\n');
return 0;
}
struct mean_array get_mean(int arr[], size_t arr_sz)
{
assert(arr_sz <= MAX_ARR);
struct mean_array res = { .num_elems = arr_sz };
double sum = 0;
for (size_t i = 0; i < arr_sz; i++) {
sum += arr[i];
res.array[i] = 10.5 * i / (128.0 - 1.0);
}
res.mean = sum / arr_sz;
return res;
}
Program output:
mean: 3.000000
0.00000 0.08268 0.16535 0.24803 0.33071
In answer to a couple of questions asked by OP in the comments:
size_t is the correct type to use for array indices, since it is guaranteed to be able to hold any array index. You can often get away with int instead; be careful with this, though, since accessing, or even forming a pointer to, the location one before the first element of an array leads to undefined behavior. In general, array indices should be non-negative. Further, size_t may be a wider type than int in some implementations; size_t is guaranteed to hold any array index, but there is no such guarantee for int.
Concerning the for loop syntax used here, e.g., for (size_t i = 0; i < sz; i++) {}: here i is declared with loop scope. That is, the lifetime of i ends when the loop body is exited. This has been possible since C99. It is good practice to limit variable scopes when possible. I default to this so that I must actively choose to make loop variables available outside of loop bodies.
If the loop-scoped variables or size_t types are causing compilation errors, I suspect that you may be compiling in C89 mode. Both of these features were introduced in C99.If you are using gcc, older versions (for example, gcc 4.x, I believe) default to C89. You can compile with gcc -std=c99 or gcc -std=c11 to use a more recent language standard. I would recommend at least enabling warnings with: gcc -std=c99 -Wall -Wextra to catch many problems at compilation time. If you are working in Windows, you may also have similar difficulties. As I understand it, MSVC is C89 compliant, but has limited support for later C language standards.
The following code is produced strange results when trying to sort of series of unsigned long ints. I have no idea why. It compiles warningless. The problem lies somewhere in my usage of qsort but I've scruntinzed that line for a few hours now. I think my comparison function is okay. I've tried semi-blindly trying all permutations I can think of to make sure I am passing the right arguments and still no dice. Any help locating my issue would be appreciated:
#include<stdio.h>
#include<stdlib.h>
#define LENGTH 10
static int cmpfunc (const void *a, const void *b) {
const unsigned long int x=*(const unsigned long int*)a, y=*(const unsigned long int*)b;
printf("x=%lu ",x); printf("y=%lu ",y);
if ( x==y ) {
puts("returning 0..."); return 0;
} else {
if ( x>y ) {
puts("returning 1..."); return 1;
} else {
puts("returning -1..."); return -1;
}
}
}
int main(void) {
/* declare the storage for our "array". Using malloc instead of [] because in
real program array size shall be dynamic */
unsigned long int *mystorage=malloc(LENGTH*sizeof(unsigned long int)); /* check for NULL */
/* fill up array with values in non-monotonic order and show the values */
for(unsigned long int counter=0;counter<LENGTH;counter++) {
*(mystorage+counter*sizeof(unsigned long int))=(unsigned long int)(counter*(LENGTH-counter));
printf("value is %lu\n",*(mystorage+counter*sizeof(unsigned long int)));
}
/* sort array */
qsort(mystorage, LENGTH, sizeof(unsigned long int), cmpfunc);
/* print out array again to see if it changed */
for(unsigned long int counter=0;counter<LENGTH;counter++) {
printf("value is %lu\n",*(mystorage+counter*sizeof(unsigned long int)));
}
exit(EXIT_SUCCESS);
}
*(mystorage+counter*sizeof(unsigned long int))
=
(unsigned long int)(counter*(LENGTH-counter));
... is incorrect. (I rearranged your whitespace a little.) It should be
mystorage[counter] =
counter*(LENGTH-counter); // the cast is redundant
The following three are equivalent:
mystorage[counter]
counter[mystorage] // yes, this weird thing is equivalent
*(mystorage+counter)
Compare the last line to your code. When you add a pointer and an integer, the compiler will already know to move by the correct number of bytes. Because you include the sizeof stuff, you have these two equivalent lines (that are different from the above three lines)
*(mystorage + counter*sizeof(unsigned long int))
mystorage[counter*sizeof(unsigned long int)]
It should be clear that these two will access outside the array bounds.
Your program is wrong in the part
for(unsigned long int counter=0;counter<LENGTH;counter++) {
*(mystorage+counter*sizeof(unsigned long int))=(unsigned long int)(counter*(LENGTH-counter));
printf("value is %lu\n",*(mystorage+counter*sizeof(unsigned long int)));
}
When you add some integers to pointer compiler automatically use address arithmetic, so you should not use *sizeof(unsigned long int). The same mistake in the printing loop. So use simple indexing mystorage[counter] or *(mystorage+counter)
I have an int array and I need to find the number of elements in it. I know it has something to do with sizeof but I'm not sure how to use it exactly.
If you have your array in scope you can use sizeof to determine its size in bytes and use the division to calculate the number of elements:
#define NUM_OF_ELEMS 10
int arr[NUM_OF_ELEMS];
size_t NumberOfElements = sizeof(arr)/sizeof(arr[0]);
If you receive an array as a function argument or allocate an array in heap you can not determine its size using the sizeof. You'll have to store/pass the size information somehow to be able to use it:
void DoSomethingWithArray(int* arr, int NumOfElems)
{
for(int i = 0; i < NumOfElems; ++i) {
arr[i] = /*...*/
}
}
int a[20];
int length;
length = sizeof(a) / sizeof(int);
and you can use another way to make your code not be hard-coded to int
Say if you have an array array
you just need to:
int len = sizeof(array) / sizeof(array[0]);
I personally think that sizeof(a) / sizeof(*a) looks cleaner.
I also prefer to define it as a macro:
#define NUM(a) (sizeof(a) / sizeof(*a))
Then you can use it in for-loops, thusly:
for (i = 0; i < NUM(a); i++)
It is not possible to find the number of elements in an array unless it is a character array. Consider the below example:
int main()
{
int arr[100]={1,2,3,4,5};
int size = sizeof(arr)/sizeof(arr[0]);
printf("%d", size);
return 1;
}
The above value gives us value 100 even if the number of elements is five.
If it is a character array, you can search linearly for the null string at the end of the array and increase the counter as you go through.
In real we can't count how many elements are store in array
But you can find the array length or size using sizeof operator.
But why we can't find how many elements are present in my array.
Because when we initialise an array compiler give memory on our program like a[10] (10 blocks of 4 size) and every block has garbage value if we put some value in some index like a[0]=1,a[1]=2,a[3]=8; and other block has garbage value no one can tell which value is garbage and which value is not garbage that's a reason we cannot calculate how many elements in an array. I hope this will help you to understand. Little concept
Super easy.
Just divide the number of allocated bytes by the number of bytes of the array's data type using sizeof().
For example, given an integer array called myArray
int numArrElements = sizeof(myArray) / sizeof(int);
Now, if the data type of your array isn't constant and could possibly change, then make the divisor in the equation use the size of the first value as the size of the data type.
For example:
int numArrElements = sizeof(myArray) / sizeof(myArray[0]);
This way, the code is type agnostic and will function correctly no matter the data type of the array.
I used following code as suggested above to evaluate number of elements in my 2-dimensional array:
#include <stdio.h>
#include <string.h>
void main(void)
{
char strs[3][20] =
{
{"January"},
{"February"},
{""}
};
int arraysize = sizeof(strs)/sizeof(strs[0]);
for (int i = 0; i < arraysize; i++)
{
printf("Month %d is: %s\n", i, strs[i]);
}
}
It works nicely. As far as I know you can't mix up different data types in C arrays and also you should have the same size of all array elements (if I am right), therefore you can take advantage of that with this little trick:
count number of bytes with sizeof() function from whole 2d array (in this case 3*20 = 60 bytes)
count number of bytes with sizeof() function from first array element strs[0] (in this case 20 bytes)
divide whole size with size of one element what will give you number of elements
This snipped should be portable for 2d arrays in C however in other programming languages it could not work because you can use different data types within array with different sizes (like in JAVA).
The question is simple: given a C++ array (e.g. x as in int x[10]), how would you get the number of elements in it?
An obvious solution is the following macro (definition 1):
#define countof( array ) ( sizeof( array )/sizeof( array[0] ) )
I cannot say this isn’t correct, because it does give the right answer when you give it an array. However, the same expression gives you something bogus when you supply something that is not an array. For example, if you have
int * p;
then countof( p ) always give you 1 on a machine where an int pointer and an int have the same size (e.g. on a Win32 platform).
This macro also wrongfully accepts any object of a class that has a member function operator[]. For example, suppose you write
class IntArray {
private:
int * p;
size_t size;
public:
int & operator [] ( size_t i );
} x;
then sizeof( x ) will be the size of the x object, not the size of the buffer pointed to by x.p. Therefore you won’t get a correct answer by countof( x ).
So we conclude that definition 1 is not good because the compiler does not prevent you from misusing it. It fails to enforce that only an array can be passed in.
What is a better option?
Well, if we want the compiler to ensure that the parameter to countof is always an array, we have to find a context where only an array is allowed. The same context should reject any non-array expression.
Some beginners may try this (definition 2):
template <typename T, size_t N>
size_t countof( T array[N] )
{
return N;
}
They figure, this template function will accept an array of N elements and return N.
Unfortunately, this doesn’t compile because C++ treats an array parameter the same as a pointer parameter, i.e. the above definition is equivalent to:
template <typename T, size_t N>
size_t countof( T * array )
{
return N;
}
It now becomes obvious that the function body has no way of knowing what N is.
However, if a function expects an array reference, then the compiler does make sure that the size of the actual parameter matches the declaration. This means we can make definition 2 work with a minor modification (definition 3):
template <typename T, size_t N>
size_t countof( T (&array)[N] )
{
return N;
}
This countof works very well and you cannot fool it by giving it a pointer. However, it is a function, not a macro. This means you cannot use it where a compile time constant is expected. In particular, you cannot write something like:
int x[10];
int y[ 2*countof(x) ]; // twice as big as x
Can we do anything about it?
Someone (I don’t know who it is – I just saw it in a piece of code from an unknown author) came up with a clever idea: moving N from the body of the function to the return type (e.g. make the function return an array of N elements), then we can get the value of N without actually calling the function.
To be precise, we have to make the function return an array reference, as C++ does not allow you to return an array directly.
The implementation of this is:
template <typename T, size_t N>
char ( &_ArraySizeHelper( T (&array)[N] ))[N];
#define countof( array ) (sizeof( _ArraySizeHelper( array ) ))
Admittedly, the syntax looks awful. Indeed, some explanation is necessary.
First, the top-level stuff
char ( &_ArraySizeHelper( ... ))[N];
says _ArraySizeHelper is a function that returns a reference (note the &) to a char array of N elements.
Next, the function parameter is
T (&array)[N]
which is a reference to a T array of N elements.
Finally, countof is defined as the size of the result of the function _ArraySizeHelper. Note we don’t even need to define _ArraySizeHelper(), -- a declaration is enough.
With this new definition,
int x[10];
int y[ 2*countof(x) ]; // twice as big as x
becomes valid, just as we desire.
Am I happy now? Well, I think this definition is definitely better than the others we have visited, but it is still not quite what I want. For one thing, it doesn’t work with types defined inside a function. That’s because the template function _ArraySizeHelper expects a type that is accessible in the global scope.
I don’t have a better solution. If you know one, please let me know.
#include<stdio.h>
int main()
{
int arr[]={10,20,30,40,50,60};
int *p;
int count=0;
for(p=arr;p<&arr+1;p++)
count++;
printf("The no of elements in array=%d",count);
return 0;
}
OUTPUT=6
EXPLANATION
p is a pointer to a 1-D array, and in the loop for(p=arr,p<&arr+1;p++)
I made p point to the base address. Suppose its base address is 1000; if we increment p then it points to 1002 and so on. Now coming to the concept of &arr - It basically represents the whole array, and if we add 1 to the whole array i.e. &arr+1, it gives the address 1012 i.e. the address of next 1-D array (in our case the size of int is 2), so the condition becomes 1000<1012.
So, basically the condition becomes
for(p=1000;p<1012;p++)
And now let's check the condition and count the value
1st time p=1000 and p<1012 condition is true: enter in the loop, increment the value of count to 1.
2nd time p=1002 and p<1012 condition is true: enter in the loop, increment the value of count to 2.
...
6th time p=1010 and p<1012 condition is true: enter in the loop, increment the value of count to 6.
Last time p=1012 and p<1012 condition is false: print the value of count=6 in printf statement.
sizeof returns the size in bytes of it's argument. This is not what you want, but it can help.
Let's say you have an array:
int array[4];
If you apply sizeof to the array (sizeof(array)), it will return its size in bytes, which in this case is 4 * the size of an int, so a total of maybe 16 bytes (depending on your implementation).
If you apply sizeof to an element of the array (sizeof(array[0])), it will return its size in bytes, which in this case is the size of an int, so a total of maybe 4 bytes (depending on your implementation).
If you divide the first one by the second one, it will be: (4 * the size of an int) / (the size of an int) = 4; That's exactly what you wanted.
So this should do:
sizeof(array) / sizeof(array[0])
Now you would probably like to have a macro to encapsulate this logic and never have to think again how it should be done:
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
You need the parentheses enclosing all the macro as in any other complex macro, and also enclosing every variable, just to avoid unexpected bugs related to operators precedence.
Now you can use it on any array like this:
int array[6];
ptrdiff_t nmemb;
nmemb = ARRAY_SIZE(array);
/* nmemb == 6 */
Remember that arguments of functions declared as arrays are not really arrays, but pointers to the first element of the array, so this will NOT work on them:
void foo(int false_array[6])
{
ptrdiff_t nmemb;
nmemb = ARRAY_SIZE(false_array);
/* nmemb == sizeof(int *) / sizeof(int) */
/* (maybe ==2) */
}
But it can be used in functions if you pass a pointer to an array instead of just the array:
void bar(int (*arrptr)[7])
{
ptrdiff_t nmemb;
nmemb = ARRAY_SIZE(*arrptr);
/* nmemb == 7 */
}
void numel(int array1[100][100])
{
int count=0;
for(int i=0;i<100;i++)
{
for(int j=0;j<100;j++)
{
if(array1[i][j]!='\0')
{
count++;
//printf("\n%d-%d",array1[i][j],count);
}
else
break;
}
}
printf("Number of elements=%d",count);
}
int main()
{
int r,arr[100][100]={0},c;
printf("Enter the no. of rows: ");
scanf("%d",&r);
printf("\nEnter the no. of columns: ");
scanf("%d",&c);
printf("\nEnter the elements: ");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
scanf("%d",&arr[i][j]);
}
}
numel(arr);
}
This shows the exact number of elements in matrix irrespective of the array size you mentioned while initilasing(IF that's what you meant)
we can find number of elements in array only if array is declared in this format
int a[]={1,2,3,4,5,6};
number of element in array is
n=sizeof(a) / sizeof(a[0]);
we should no able to calculate array size if it is declared like this int a[10]={1,2,3,4,5,6}
i mostly found a easy way to execute the length of array inside a loop just like that
int array[] = {10, 20, 30, 40};
int i;
for (i = 0; i < array[i]; i++) {
printf("%d\n", array[i]);
}
If we don't know the number of elements in the array and when the input is given by the user at the run time. Then we can write the code as
C CODE:
while(scanf("%d",&array[count])==1) {
count++;
}
C++ CODE:
while(cin>>a[count]) {
count++;
}
Now the count will be having the count of number of array elements which are entered.
Assuming you have an array with elements 1,3,4.
To know its length, you'd need to use the sizeof function as follows:
int myArray[] = {1,3,4};
int len = sizeof(myArray) / sizeof(myArray[0]);
You can check the number of elements by printing the output as follows:
cout<<"This array has " << len << " elements";
The full program would be as follows:
#include <iostream>
using namespace std;
int main()
{
int myArray[] = {1,3,4};
int len = sizeof(myArray) / sizeof(myArray[0]);
cout<<"The array has " << len << "elements";
return 0;
}
Actually, there is no proper way to count the elements in a dynamic integer array. However, the sizeof command works properly in Linux, but it does not work properly in Windows. From a programmer's point of view, it is not recommended to use sizeof to take the number of elements in a dynamic array. We should keep track of the number of elements when making the array.