Total members 10249 | Gratitudes |It is currently Thu May 17, 2012 8:47 am Login / Join Codemiles


All times are UTC [ DST ]




Post new topic Reply to topic  Quick reply  [ 1 post ] 
Author Code Snippet
 Code subject: Array Passing
PostPosted: Thu Nov 13, 2008 2:54 pm 
Offline
Beginner
User avatar

Joined: Sun May 25, 2008 5:34 pm
Posts: 95
Has thanked: 2 time
Have thanks: 1 time

Code:
/*
* Demonstrate some pecularities of array passing in C.
*
* Author: Tom Bennet
*/
#include <iostream>

using namespace std;

// Each version receives the same size-5 integer array from the main pgm.

// Receive it as declared in main.
void rcvA(int arr[5])
{
        cout << "A [" << sizeof arr << "]" << endl;
        for(int m = 0; m < 5; m++)
                cout << arr[m] << " ";
        cout << endl;
}

// Receive it as the system sends it -- a pointer to the first item in the
// array.
void rcvB(int *arr)
{
        cout << "B [" << sizeof arr << "]" << endl;
        for(int m = 0; m < 5; m++)
                cout << arr[m] << " ";
        cout << endl;
}

// Since it sends a pointer and ignores the array size anyway, the system
// lets you leave it out here, too.
void rcvC(int arr[])
{
        cout << "C [" << sizeof arr << "]" << endl;
        for(int m = 0; m < 5; m++)
                cout << arr[m] << " ";
        cout << endl;
}

// In fact, so long as the system is going to ignore the size anyway, we can
// write what we want.  The 1024 doesn't make the array that large,
// however; it's however large its creator made it.  rcvD can't find out.
void rcvD(int arr[1024])
{
        cout << "D [" << sizeof arr << "]" << endl;
        for(int m = 0; m < 5; m++)
                cout << arr[m] << " ";
        cout << endl;
}

main()
{
        // Here's the array all the fuss is about.
        int arr[5];

        // Put some stuff there.
        for(int i = 0; i < 5; ++i)
                arr[i] = 2*i -3;

        // Do exactly what the functions do.
        cout << "M [" << sizeof arr << "]" << endl;
        for(int m = 0; m < 5; m++)
                cout << arr[m] << " ";
        cout << endl;

        // Now run the functions.
        rcvA(arr);
        rcvB(arr);
        rcvC(arr);
        rcvD(arr);
}


passing arrays to functions in c++ :grin:


TOP
 Profile Send private message  
Reply with quote  
Post new topic Reply to topic Quick reply  [ 1 post ] 
Quick reply


  

 Similar topics
 search for string in a cell array
 Shuffle Array
 check array contains a value using javascript
 Array size to zero
 array in oracle database
 How to write C++ code that defines array of size 24 then;
 access element in array C++
 Average of an array. Please help
 need some help with array comparing program (solved)
 PHP SOAP server returns array using NuSOAP Library

All times are UTC [ DST ]


Users browsing similar codes

Users browsing this forum: No registered users and 1 guest



Jump to:  
Previous Code Snippet | Next Code Snippet 




Home
General Talks
Finished Projects
Code Library
Games
Tutorials

Java
C/C++
C-sharp
php
Script
JSP/Servlets
Ajax
ASP/ASP.net
Google SEO
Database
Communications
Phpbb3 styles
Photoshop tutorials
Flash tutorials
Find a job






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team