Passing an array in a funciton

This is a discussion on Passing an array in a funciton within the C Programming forums, part of the General Programming Boards category; How do I pass an array as a a parameter in a function? I don't want to pass the reference/pointer ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    75

    Passing an array in a funciton

    How do I pass an array as a a parameter in a function?
    I don't want to pass the reference/pointer to the array.


    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    Wrap the array in a struct type, then pass such a struct to the function as an argument.
    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
    Feb 2009
    Posts
    278
    Why not? That is the way to do it easily.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    #include <stdio.h>
    Code:
    void printarr(int a[]) {
        	a[0] = 9;
            printf(" %d\n",a[0]);
        
    }
    
    main() {
        int a[5];
        int i;
        
        a[0] = 2;
        printarr(a);
        printf("a: %d\n",a[0]);  
    }

    That's passing a pointer

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,641
    Quote Originally Posted by jordanguyoflove
    That's passing a pointer
    Yes, and generally you do want to do that. Even if you take my suggestion, you probably should then pass a pointer to the struct, precisely to avoid possibly expensive copying.
    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

  6. #6
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    you can also pass individual elements of the array to a function but changes made in the function will not be visible by the calling function.heres an example

    Code:
    int main()
    {
    int a[]={10,20,30}
    for(i=0;i<=2;i++)
    display(a[i]);
    }
    void display(int m)
    {
    printf("%d",m);
    }
    Last edited by BEN10; 03-31-2009 at 10:19 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. help with passing 2d array...
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 02-05-2002, 01:06 PM

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