Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7589587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:11:54+00:00 2026-05-30T20:11:54+00:00

I am a beginner in C++ stdlib. I learnt stdlib tutorials and I am

  • 0

I am a beginner in C++ stdlib. I learnt stdlib tutorials and I am implementing “number of connected components in Graph” using adjacency list created with stdlib lists. I wanted to know how to pass this array of list by reference to dfs function? Also, one of my frnd said that by default it will be passed by reference. Is it true? Please clarify. which of these is right?

for example:

  1. My array of list: list<int> L[v];
  2. My function call: dfs(L[v],k);
  3. My function definition: void dfs(list<int> List, int index);
  4. My function prototype: void dfs(list<int> L, int);

(or)

  1. My array of list: list<int> L[v];
  2. My function call: dfs(L,k);
  3. My function definition: void dfs(list<int> *L, int index);
  4. My function prototype: void dfs(list<int> *, int);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:11:56+00:00Added an answer on May 30, 2026 at 8:11 pm

    I wanted to know how to pass this array of list by reference to dfs function? Also, one of my frnd said that by default it will be passed by reference.

    Not quite.

    First of all, let’s forget about the std::list; it’s just confusing matters.

    Pretend you’re passing an array of int instead:

    void foo(int[] x);
    
    int main() {
       int x[5];
       foo(x);
    }
    

    There are no references here, and C++ arguments are copied by default, but because arrays cannot be copied and because the name of an array decays to the name of a pointer to the first element in the array, you’re passing a [copy of a] pointer not the array itself.

    In fact, void foo(int[] x) is misleading syntactic sugar for the equivalent, and clearer void foo(int* x).

    In particular note that — in both cases — the function foo does not know the dimension of the original array.

    This is kind of old-fashioned, though, and you can pass an actual reference to an array:

    void foo(int (&x)[5]);
    
    int main() {
       int x[5];
       foo(x);
    }
    

    Now we can apply this same logic to arrays of std::list:

    void foo(std::list<int>*); // pointer to one or more lists, OR
    void foo(std::list<int> (&)[5]); // reference to an array of five lists
    

    Anyway, mixing standard containers and arrays seems odd; prefer a std::vector over an array, or a wrapper around statically-allocated arrays (like std::array or, previously, boost::array) if you really need the automatic storage duration for some reason.


    Also, your call syntax is wrong.

    My function call: dfs(L[v],k);
    My function definition: void dfs(list List, int index);

    This passes a single list from the array of lists, and does so by value/copying.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Beginner programmer here....hope it makes sense :) I have created a console app that
Beginner question: I have a dictionary where the values are lists of (a variable
iOS beginner here. I'm using the following code to save my facebook accessToken and
Beginner Git question: In the Mercurial world, hg tags gives me a list of
Beginner jquery question. I am using the Tokenizing Autocomplete Text Entry plugin, with the
beginner to python here. I have 2 nested lists that I want to merge:
Absolute beginner question here. I have two lists in mathematica. The first one was
Beginner level question Scenario: Have simple string cocantation tool, that I might expand later
Beginner help needed :) I am doign an example form a php book which
A beginner question, bear with me: I'm just wondering under what circumstances one should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.