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

  • SEARCH
  • Home
  • 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 6324133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:38:53+00:00 2026-05-24T16:38:53+00:00

i have a specific problem with pointers and references, with std::vector and std::string. Some

  • 0

i have a specific problem with pointers and references, with std::vector and std::string. Some question are in the following code snipped, some below.

I have basically this code

//first of all: is this function declaration good?
//or shoudl I already pass the std::vector in another way?
//I'm only reading from it
void func(const std::vector<std::string>& vec)
{
    //I need to call otherFunc(void*) with  a single element from vec:
    //otherFunc(void* arg);
    //with arg being a void* to vec[0]
}

My IDE tells me that only &*vec[0] works as parameter for otherFunc, but this doesn’t compile…

How is the best way to do these kind of parameter passing?

  • 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-24T16:38:54+00:00Added an answer on May 24, 2026 at 4:38 pm

    That is a good declaration, as long as the function is not intended to modify the vector. It’s more efficient than passing by value, since it avoids copying the vector – an expensive operation requiring a memory allocation.

    However, the other function requires a non-const pointer. How to handle this depends on whether it might modify the data.

    If it won’t (as you imply when you say “I’m only reading from it”) then the options are:

    • Change it to otherFunc(void const * arg) to give a stronger guarantee that it won’t, or
    • Remove the const qualification with a const_cast<void*> when calling it

    Note that &*vec[0] won’t compile; you want vec[0].c_str() to get a C-compatible pointer to the first string’s data, assuming that’s what you need.

    If it might modify the vector, then you’ll have to do something else, since there’s no legal way to modify a std::string through a pointer to its data. Probably the best option is to use std::vector<char> rather than std::string, but that depends on exactly what the function does.

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

Sidebar

Related Questions

I have this code to represent bank: class Bank { friend class InvestmentMethod; std::vector<BaseBankAccount*>
I have run into a bit of a problem here: I had a problem-specific
I have the problem that an specific step in Ant can only be executed
We have a problem in a specific server. All plus signs posted to the
I have a problem with a specific java client library. Here is the situation:
I have a specific case in mind, but the question applies in general too.
I have a specific question, that could use a general answer... When building Multi-Tier
I have some specific id like 1,2,5,11,64589 in solr (int type) I want to
I have the following problem in application architecture and am willing to solve it
I have some Visual C++ code that receives a pointer to a buffer with

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.