Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
i tried this code

C++
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace::std;
// The called function //
void rando1(int a[])
{
    for(int j=0;j<5;j++)
    {
    
        cout<<a[j]<<endl;
        cout<<endl;
    }
}

main()
{
    int arr[4];
    for(int i=0;i<5;i++)
    {
        cout<<"Enter 5 numbers: ";
        cin>>arr[i];
    }
    cout<<endl;
    // The calling function //
    rando1(arr);
    getch();
}




it works

but i also tried changing the data type int into a string but i didnt work. how can i pass string arrays to a function?

Thank you for helping
Posted
Updated 6-May-15 5:24am
v2
Comments
Sergey Alexandrovich Kryukov 6-May-15 11:25am    
Where are your string objects and what's the problem? You can pass it the same way as any other objects or arrays of objects...
—SA
Reuben Cabrera 6-May-15 18:50pm    
i just changed the data type in the program from int to string. didnt work.
Albert Holguin 6-May-15 18:51pm    
Upvoted the question because it's a legitimate question and the OP is actually trying. Not sure who/why it was downvoted. (+4)

You are corrupting memory with this code. The elements of a 4 element array lie in the range [0..3] not [0..5]. And passing strings is exactly the same as passing ints, just make sure the function parameter type is correct, e.g.
C++
void rando1(string a[])
{
    for(int j = 0; j < 4; j++)
    {
        cout << a[j] << endl;
        cout << endl;
    }
}
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 6-May-15 11:33am    
+5 :)
CPallini 6-May-15 12:04pm    
5.
Albert Holguin 6-May-15 18:52pm    
+5
Reuben Cabrera 6-May-15 18:53pm    
doesn't work :(
Richard MacCutchan 7-May-15 3:59am    
What doesn't work?
Modern C++ can be quite expressive:

C++
#include <iostream>
#include <string>
#include <array>
using namespace std;

template <typename T, size_t N> void show_array( array <T,N> & a)
{
  for (const auto & x : a) cout << x << endl;
}

int main()
{

  array<int,3> ai = { -3 ,17, 0};
  array<string,2> as= {"foo", "bar"};

  show_array(ai);
  show_array(as);
}



A more (more) traditional approach:
C++
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;

void show_int_array(int ai[], size_t size)
{
  for (size_t n = 0; n < size; ++n)
    cout << ai[n] << endl;
}
void show_string_array(string as[], size_t size)
{
  for (size_t n = 0; n < size; ++n)
    cout << as[n] << endl;
}


int main()
{
  int ai[] = {3, -5, 7};
  string as[] = { "foo", "bar" };

  show_int_array(ai, sizeof(ai)/sizeof(ai[0]));
  show_string_array(as, sizeof(as)/sizeof(as[0]));
}


The code redundancy of show_int_array and show_string_array immediately suggests the usage of a template:

C++
#include <cstddef>
#include <iostream>
#include <string>
using namespace std;

template <typename T> void show_array(T a[], size_t size)
{
  for (size_t n = 0; n < size; ++n)
    cout << a[n] << endl;
}

int main()
{
  int ai[] = {3, -5, 7};
  string as[] = { "foo", "bar" };

  show_array(ai, sizeof(ai)/sizeof(ai[0]));
  show_array(as, sizeof(as)/sizeof(as[0]));
}


'Minimalist' approach
C++
#include <iostream>
#include <string>
using namespace std;

int main()
{
  int ai[] = {3, -5, 7};
  string as[] = { "foo", "bar" };

  for (auto x : ai) cout << x << endl;
  for (auto s : as) cout << s << endl;
}
 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 6-May-15 15:07pm    
Nice, a 5 (only better remove all those <h1>...)
—SA
CPallini 6-May-15 15:44pm    
Thak you.
I tried my best in order to remove them. In fact they don't even appear in the code while I'm editing, I see them only after the submit.
Afzaal Ahmad Zeeshan 6-May-15 15:56pm    
There you go CPallini. Hope you don't mind the imminent edit. ;)
CPallini 6-May-15 16:11pm    
Thank you very much.
Sergey Alexandrovich Kryukov 6-May-15 16:43pm    
I can see some glitched in posting these days, but usually the artifacts can be removed after next couple of attempts. :-)
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900