little help with passing by value please

This is a discussion on little help with passing by value please within the C Programming forums, part of the General Programming Boards category; If I pass an array to a function, it's actually passing a pointer and thus passing by reference. How do ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    3

    Question little help with passing by value please

    If I pass an array to a function, it's actually passing a pointer and thus passing by reference. How do I pass an array to a function by value?

    Thanks
    ===============
    Code:
    #include <stdio.h>
    #define MAXVAL 10
    
    void modify_array_pointer(int*, int);
    void print_array(int[], int);
    
    int main()
    {
      int numbers[MAXVAL];
      for (int i=0; i < MAXVAL; i++)
      {
          numbers[i] = i;
      }
    
      print_array(numbers, MAXVAL);
      modify_array_pointer(numbers, MAXVAL);
      print_array(numbers, MAXVAL);
    
    
      return 0;
    }
    
    void modify_array_pointer(int *p, int maxval)
    {
      for (int i=0; i < maxval; i++)
      {
        p[i]+=2;
      }
    }
    
    void print_array(int A[], int maxval)
    {
      for (int i=0; i < maxval; i++)
      {
        printf("%3d , ", A[i]);
      }
      printf("\n");
    }

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You can't

    edit: You could however, make it a global array, put it in a structure, and pass a pointer to the structure, or just make do with passing by reference, which is entirely possible.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > How do I pass an array to a function by value?
    You don't - arrays are always passed as a pointer to the first element of the array.

    If you want to protect the array from being modified, then declare
    void print_array(const int[], int);
    You can still modify it, but not without the compiler complaining about it.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing struct vs. reference
    By reakinator in forum C Programming
    Replies: 4
    Last Post: 06-14-2008, 10:11 PM
  2. Newb Question on Passing Objects as Parameters
    By Mariano L Gappa in forum C++ Programming
    Replies: 12
    Last Post: 11-29-2006, 12:08 PM
  3. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  4. Compiler "Warnings"
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-24-2005, 01:09 PM
  5. passing by address vs passing by reference
    By lambs4 in forum C++ Programming
    Replies: 16
    Last Post: 01-09-2003, 12:25 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21