?
Solved

table passed to function "by value"

Posted on 1999-07-06
5
Medium Priority
?
164 Views
Last Modified: 2012-05-04
Is it possible in C++ to pass a table of integers to some function "by value"? so that even when I change the table in that specific function it will remain unchanged in the "main" program?
0
Comment
Question by:wrewers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Author Comment

by:wrewers
ID: 1199233
Edited text of question.
0
 

Author Comment

by:wrewers
ID: 1199234
I meant something like this:

int suma(int tab[4])
{
  int i,outcome=0;
  for(i=0;i<3;++i) outcome+=tab[i];
  tab[3]=outcome;
  return outcome;
};

int main(void)
{
  int t[4]={1,2,3};
  suma(t);
  return 0;
};
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 1199235
This will work properly if you use std::vector
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 1199236
You can't do it with simple arrays in the way you want (remember as far as the compiler is concerned there is no difference between an array and a pointer:

  array[i] is equivalant to *(array+i)

if you want to pass C arrays by value you would have to make a copy in the function that wants it.  (This can be fairly efficient, because at least with simple C arrays you can use memcpy or something like it).

The best thing is to do as KangaRoo suggests, use a better container than a simple array, and the STL vector is probably the container of choice for this sort of thing.
0
 
LVL 2

Accepted Solution

by:
curri earned 150 total points
ID: 1199237
Yo can NOT pass an array by value; however you can put the array inside a structure and pass that structure by value.

like
struct cheat {
      int t[4];
};

cheat t={{1,2,3,4}}
etcetera

0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question