11

I am new to C++ and I am writing the following code. I needed to iterate over all the addons in my calling function - testFunction. I know this works in C#, but this code is not working. Can anyone please point out the right way to do it in C++?

#include "stdafx.h"
#include <iostream>
#include "resource.h"

int testFunction(char* tester);
int _tmain()
{
    int mainProd=2;
    int Addons[]={7,8,9,10};

    testFunction(mainProd,Addons);


}
void testFunction(int mainProd,int addons[])
{
    for(int x = 0 ; addons.length;++x) ---- Not working 
    {
        std::cout<< addons[x];
    }
}

I tried to implement vector as below suggestions by you guys

#include "stdafx.h"
#include <iostream>
#include "resource.h"
#include <vector>

void testFunction(std::vector<int> addons);

int _tmain(int argc, _TCHAR* argv[])
{

    std::vector<int>  Addons ;
    for(int i = 0 ;i<10;++i)
    {
        Addons.push_back(i);
    }
     testFunction(Addons);
}

void testFunction(std::vector<int> addons)
{
    for(int i =0 ; i<addons.size();++i)
    {
        std::cout<<addons.at(i);
    }
}
9
  • 7
    Where did you get the idea that .length would work? Perhaps you need to pick a better book? Commented Jan 15, 2013 at 13:21
  • What does your favourite C++ book say? Commented Jan 15, 2013 at 13:24
  • 4
    fordward declare the correct version of testFunction too. Commented Jan 15, 2013 at 13:26
  • 1
    pass by reference the vector in order to avoid unnecessary copys void testFunction(const std::vector<int> &addons) // note the 'const' and the '&' Commented Jan 15, 2013 at 14:46
  • 2
    for(int i =0 ; i<addons.size();++i) is bad style for iterating over a vector. Use begin and end instead. Commented Jan 15, 2013 at 15:27

6 Answers 6

20

An array (a raw array) decays into a pointer when passed as an argument to a function, so your array has no size information.

You need to pass the length of the array explicitly into the function to know it inside the function.

Alternatively, and better, use a std::vector and then you'll have the .size() always available when needed.

Sign up to request clarification or add additional context in comments.

3 Comments

Or std::array if the size is fixed at compile-time.
Because you have an function with a char* parameter declared before your main and only defined the correct one after. ideone.com/2H1xX8
or pass an array by reference
7

Apart from using vectors, as Tony suggests, you can use templates and pass the array by reference so that the compiler will deduce the array's size:

template<int N>
void testFunction(int mainProd,int (&addons)[N])
{
    for(int x = 0; x < N; ++x) // ---- working 
    {
        std::cout<< addons[x];
    }
}

Comments

3

You're using concepts of C# in C++ but, even if we assume that both languages are similar, they're not equal.

The syntax for a ranged-for in C++ is the following:

for (type identifier : container) // note the ':', not ';'
{
    // do stuff
}

You can use this for flavour if you have a C++11 compiler.

Btw, it seems that you're using properties on your code:

for(int x = 0 ; addons.length;++x) // what is lenght?
{
    std::cout<< addons[x];
}

There's no such thing in C++, if you want to call an object method you need to call it as a function:

// assuming that the object 'addons' have a method
// named 'length'  that takes no parameters
addons.length();

But the addons variable isn't an object, is an array (take a look to this tutorial), so it doesn't have a method or property named length; if you need to know its length in order to iterate it you can use in some contexts the sizeof operator (see the tutorial for more information).

Let's suppose that addons were a container:

typedef std::vector<addon> Addons;
Addons addons;

If you want to iterate it using the C++11 range-for, you can write it as follows:

for (addon a : addons)
{
    // do stuff with a.
}

Hope it helps.

2 Comments

There are a lot of typos in this answer.
@AlexChamberlain could you help me to fix it? i don't found any typos, maybe you're referring to my lack of english skills?
2

If you were to use a std::vector or std::array, you could use std::foreach,

std::vector<int> addons = {7,8,9,10};
std::array<int, 4> addons = {7,8,9,10}; // Only use one of these...
std::foreach(addons.begin(), addon.end(), [](int i) {
    std::cout << i
});

6 Comments

The lambda should take int, not an iterator
@ArmenTsirunyan My mistake, sorry.
Might as well use the new range-for then.
@MarcGlisse You could, but I don't like the syntax; not as readable.
std::foreach vs range-for could be a matter of taste?
|
1

Code is working with this

for (int i = 0; i < (end(array) - begin(array)); i++)

Return maximum size

Test whether array is empty

array::empty

Element of array

array::size

Array size

sizeof()

Comments

1

If you don't want to use any STL container, you just need to pass the array by reference to the function. Problem here is that you can't define such argument without exact size of the array. This restriction you can overcome making the function as a template, defining the size as the template parameter:

#include <iostream>

template<int N>
void testFunction(int mainProd,int (&addons)[N])
{
    for(int x = 0 ; x < N; ++x)
    {
        std::cout<< addons[x];
    }
}

int main()
{
    int mainProd=2;
    int Addons[]={7,8,9,10};

    testFunction(mainProd,Addons);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.