I have something like this code and I would like to know how to get the number of elements of the array I pass in, if th

I have something like this code and I would like to know how to get the number of elements of the array I pass in, if th

Content Index :

I have something like this code and I would like to know how to get the number of elements of the array I pass in, if th
Tag : c , By : lili
Date : November 28 2020, 11:01 PM

will be helpful for those in need So int size does not actually give me the size of what I pass in, in the main function. How do I get the exact number of elements in the array that I pass in. , Pass it as parameter to myFunc.
#include <stdio.h>

void myFunc(int *array, size_t size)
    printf("Size = %zu\n", size);

    for (size_t i=0; i<size; i++)
        printf("array[%zu] = %d\n", i, array[i]);

int main(void)
    int array[]={1,2,3,4,5,6,7,8,9,10,11,12,13};

    myFunc(array, sizeof(array)/sizeof(array[0]));

    return 0;

No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

How do you Iterate a multidimesional array without knowing the number of dimensions and elements of the array being pass

Tag : chash , By : user157138
Date : March 29 2020, 07:55 AM
should help you out Would something like this work for you? It recurses the ranks so you can use a foreach() and get an array containing the current item's indices.
class Program
    static void Main(string[] args)
        int[, ,] theArray = new int[2, 8, 12];
        theArray[0, 0, 1] = 99;
        theArray[0, 1, 0] = 199;
        theArray[1, 0, 0] = 299;

        Walker w = new Walker(theArray);

        foreach (int i in w)
            Console.WriteLine("Item[{0},{1},{2}] = {3}", w.Pos[0], w.Pos[1], w.Pos[2], i);


    public class Walker : IEnumerable<int>
        public Array Data { get; private set; }
        public int[] Pos { get; private set; }

        public Walker(Array array)
            this.Data = array;
            this.Pos = new int[array.Rank];

        public IEnumerator<int> GetEnumerator()
            return this.RecurseRank(0);

        private IEnumerator<int> RecurseRank(int rank)
            for (int i = this.Data.GetLowerBound(rank); i <= this.Data.GetUpperBound(rank); ++i)
                this.Pos.SetValue(i, rank);

                if (rank < this.Pos.Length - 1)
                    IEnumerator<int> e = this.RecurseRank(rank + 1);
                    while (e.MoveNext())
                        yield return e.Current;
                    yield return (int)this.Data.GetValue(this.Pos);

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
            return this.RecurseRank(0);

How to remove duplicate code if I create array with different number of elements?

Tag : chash , By : Thomas Plunkett
Date : March 29 2020, 07:55 AM
this will help I have some duplicate code: , First thing first ,
 1- move the export and simple display functionality into 2 different methods.
 2- Create a Client Object exactly once by using Singleton Pattern.

Console output does not show number of elements of array. Array Elements not accessible in code

Tag : javascript , By : Der Ketzer
Date : March 29 2020, 07:55 AM
help you fix your problem put await before axios.all(...).then(...) to wait for all the promises before doing the dispatch:
await axios.all(promises).then(function(results) {
  results.forEach(function(response) {

How to pass the number of elements of an array to a function

Tag : c , By : Pradeep Gowda
Date : March 29 2020, 07:55 AM
it fixes the issue I would like to pass a n number of elements part of an array to a function in order to calculate the average. Essentialy i would like to keep the number of elements dynamic in the code so that a custom number can be entered. , In main
  float num[n]; 
 int n,i,k; 
 float num[n]; 
 printf("Enter the numbers of elements: "); 
 int i,k; 

 printf("Enter the numbers of elements: "); 
 if (scanf("%d",&k) != 1) {
   puts("invalid size");
   return -1;
 float num[k]; 
n = sizeof(num)/sizeof(int);
 float average(float num[], int n); 
printf("Average = %.2lf",average(num, k)); 
float average(float num[], int n)
  int i;
  float sum = 0.0;

  for(i = 0; i < n; ++i)
    sum += num[i];

  return sum / n;

How do I pass std::array as a template parameter with a varying number of elements in C++?

Tag : cpp , By : MikeW
Date : January 02 2021, 06:48 AM
I wish did fix the issue. You just specify the number of elements inside the template parameters.
template<class T, size_t N>
void func(std::array<T, N> arr) {

Related Posts Related QUESTIONS :
  • Is there a way to find the position of a special character '\n' in a file in C?
  • Array is printing with weird question mark symbols when printing fibonacci sequences
  • Self provoked buffer overflow in C
  • Why my code refuse to compare the generated hashes
  • Makefile No such file or directory
  • Why is pthread_equal threadsafe?
  • Is there a way I can shorten this if statment?
  • fscanf() != EOF as exit condition of loop exits at the wrong time
  • I made a stack using linked list (without array), but my pop function is not working
  • How can I start with this problem and fix my code?
  • How to identify which file implicitly relies on linkage with a library?
  • Does a stray \ [backslash] have any meaning in C code?
  • strtok returns NULL despite not having reached the end of the string
  • Is 0x9B (155decimal) a special control character? Why is it missing from ascii tables?
  • windows C system call with spaces in command
  • FFmpeg C API documentation/tutorial
  • Thread scheduling Round Robin / scheduling dispatch
  • Define integer ranges in C
  • feof() in C file handling
  • How to check for signed integer overflow in C without undefined behaviour?
  • Display processes that access a folder
  • How to printf a time_t variable as a floating point number?
  • Parenthesis operator in C. What is the effect in the following code
  • Is there table with timing(cost) of C functions?
  • How can I access a global pointer outside of a C function?
  • Are fopen/fread/fgets PID-safe in C?
  • How can multiple variables be passed to a function cleanly in C?
  • How to use pthread_atfork() and pthread_once() to reinitialize mutexes in child processes
  • Find function call in a list of binaries
  • How to return a single variable from a CUDA kernel function?
  • How to get number of cores in Win32?
  • Handling aces and finding a segfault in a blackjack program
  • function prototype declarations
  • How to convert from Hex to Decimal using only Integers in C
  • How to parse from char array using yacc?
  • I can not work with linked list inside linked list
  • Finding the index of largest number
  • Decompose a number to product of prime numbers and print it like 18=2*3^2
  • programming EXACT timer which calles a function in c language every x seconds
  • Combining macros generate errors
  • Why am I not getting error when I store values of different types in an array in C language?
  • What is the purpose of the base case dp[0] = -1 of this DP array in the palindromic partitions problem?
  • Having function choose largest number of given ones
  • Data structures layout
  • How to retrieve probuf from C library
  • Printf float number in hexadecimal format returns random vlaue
  • Memory allocation in C from 1 to N
  • Emit signal from separate thread in glib/gtk
  • How to fix Valgrind "Conditional Jump..." error
  • The text to be printed is being printed on the command line rather than print space while using fork on terminal
  • Deleting an element from an array of structures
  • How to read time stamp data from text file using C code?
  • I can't understand this code? Is it normal or I'm not ready?
  • GTK character count vs byte index
  • Summing up integers in an array and storing the result in an array
  • JITtting with limited knowledge of the target architecture
  • Why does global pointers behave erratically in this function?
  • Write a basic half pyramid pattern program
  • Read memory in C
  • void affecting output of the program
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com