top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Why arrays could not be passed by values to functions?

+2 votes
440 views
Why arrays could not be passed by values to functions?
posted Jun 11, 2014 by Prithvi

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

The origin is historical. The problem is that the rule "arrays decay into pointers, when passed to a function" is simple. Copying arrays would be kind of complicated and not very clear, since the behavior would change for different parameters and different function declarations.

There could be 3 main reasons why arrays are passed as pointers in C instead of by value.

  1. C is evolved from B and BCPL, where arrays were actually implemented as a pointer to the array data
  2. Efficiency
  3. No Size information (for dynamically allocated arrays)allocated arrays)
answer Jun 11, 2014 by Deepak D
Similar Questions
+2 votes

What are the languages being used for embedded system? And why these languages has been chosen ?

+2 votes

I know of "select()" which works using file descriptors.

However I want similar functionality to "select()" but for a simple function that returns an int.

So suppose, there's "int calculate_magic()" function and I want to allow it to work for 2 seconds, if it takes longer than that I want to move on.

If "calculate_magic()" wrote its answer to a file descriptor I could use "select()" but how to do it for the case I mentioned?

I need something like this for a simple game implementation where the player is allowed a maximum time to make a decision about their next move.
I
was hoping I can get something without having to deal with threads, etc.

0 votes

How we can add two 64 bit number on 32 bit machine using c/c++ program ?

...