Thread: How to pass an array by value in C

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    54

    How to pass an array by value in C

    I am passing the one element while I want to pass all the elements without a pointer. How to pass an array by value in C

    Code:
    #include<stdio.h>
    
    void Display(int a);
    
    
    int main()
    {
        int Arra[] = { 2, 3, 4 };
        Display(Arra[1]);        //Passing array element  only.
        return 0;
    }
    
    
    void Display(int a)
    {
        printf("%d", a);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,552
    You could wrap the array in a struct... but then you would want to use a pointer because the struct object would be expensive to copy. So don't bother: use a pointer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    54
    Quote Originally Posted by laserlight View Post
    You could wrap the array in a struct... but then you would want to use a pointer because the struct object would be expensive to copy. So don't bother: use a pointer.
    Does this mean that we cannot pass the array by its value?

    If we have to pass Array, then we have to pass its location so we have to use the pointer that would pass location of array element

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,552
    Yes, an array will always be converted to a pointer to its first element when passed as an argument to a function.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,749
    Why do you feel you need to pass the array by value?

    If you must, put the array in a struct, since you can pass these by value.
    But you need to understand that 'by value' means 'make a copy', which could be expensive for large arrays.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-20-2019, 02:00 PM
  2. Replies: 2
    Last Post: 01-10-2016, 01:23 AM
  3. Replies: 3
    Last Post: 05-09-2012, 06:41 AM
  4. Replies: 1
    Last Post: 10-21-2007, 07:44 AM
  5. Replies: 3
    Last Post: 04-02-2002, 01:39 PM

Tags for this Thread