Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Illustrating Different Ways to Pass Arrays to a Function: #1587

Open
11gurmeet11 opened this issue Aug 29, 2024 · 1 comment
Open

Illustrating Different Ways to Pass Arrays to a Function: #1587

11gurmeet11 opened this issue Aug 29, 2024 · 1 comment

Comments

@11gurmeet11
Copy link

#include < iostream >

using namespace std;

// passing array as a sized array argument.

void printArraySized(int arr[3], int n)
{
cout << "Array as Sized Array Argument: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}

// passing array as an unsized array argument.

void printArrayUnsized(int arr[], int n)
{
cout << "Array as Unsized Array Argument: ";
for (int i = 0; i < n; i++) {
cout << *(arr + i) << " ";
}
cout << endl;
}

// Passing array as a pointer argument.

void printArrayPointer(int* ptr, int n)
{
// Print array elements using pointer pointer.
// that store the address of array passed.

cout << "Array as Pointer Argument: ";
for (int i = 0; i < n; i++) {
    cout << ptr[i] << " ";
}

}

int main()
{

int arr[] = { 10, 20, 30 };

// Call function print Array and pass array and its size to it.
printArraySized(arr, 3);
printArrayUnsized(arr, 3);
printArrayPointer(arr, 3);

return 0;

}

Output ☞

Array as Sized Array Argument :10 20 30
Array as Unsized Array Argument: 10 20 30
Array as Pointer Argument: 10 20 30

Copy link

Hello @11gurmeet11, thanks for creating your first issue at LearnCPP, hope you followed the guidelines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant