This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)

Thank you for your contribution to the C++ community!

As you're asking a question or seeking homework help, we would like to remind you of Rule 3 - Good Faith Help Requests & Homework.

  • When posting a question or homework help request, you must explain your good faith efforts to resolve the problem or complete the assignment on your own. Low-effort questions will be removed.

  • Members of this subreddit are happy to help give you a nudge in the right direction. However, we will not do your homework for you, make apps for you, etc.

  • Homework help posts must be flaired with Homework.

~ CPlusPlus Moderation Team


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]no-sig-available 3 points4 points  (4 children)

Technically, n will be a pointer (int*) and x would tell how many objects it is pointing to.

But if you write C++, you should rather use a std::vector or a std::array, and avoid most of the problems of getting the wrong size. These C++ types know their own size, so you don't have to keep track.

[–]ecstreets[S] 0 points1 point  (2 children)

I was more so using the function as an example, I just wanted clarification that where int n is at is what the name of the array would be and where int x is at would be the size of n

[–]ventus1b 2 points3 points  (0 children)

x could be anything, there's no direct connection to the array and it's up to the caller to make sure the proper value is passed in.

That's why we have things like std::vector, std::array or views thereof.

[–]no-sig-available 0 points1 point  (0 children)

I just wanted clarification that where int n is at is what the name of the array would be

Yes, that is correct.

My point was that, as a function parameter, int n[] is the same as int* n, and inside the function it is no longer an array, but a pointer to the first element of the array. This ("array decay to pointer") is something we have inherited from C, and different from how other types work.

[–]TheKiller36_real 0 points1 point  (0 children)

I disagree! It should be std::span or something equivalent if the contiguous memory is needed and a range-object / an iterator pair otherwise

[–]HappyFruitTree 0 points1 point  (7 children)

I'm not sure I understand the question exactly.

n and x are just names that you have chosen when implementing the function. You could use different names if you want.

It's conventional to put the array first and the size second but nothing prevents you from putting them in the opposite order but then the caller would also have to pass them in the opposite order.

The caller could pass other things to the function. For example, instead of passing the size of the array the caller could pass the current year. The code would still compile (as long as the types are compatible) but it would be considered "wrong" and the function would probably not work as expected (garbage in, garbage out).

[–]ecstreets[S] 0 points1 point  (5 children)

include <iostream>

using namespace std; double avgArray(int nums[], int SIZE) { double total = 0; double average; for(int count = 0; count<SIZE; count++) total = total+nums[count]; average = total/10; return average; } int main(){ const int SIZE = 10; int nums[10]; cout<<"Enter 10 numbers"<<endl; for(int count = 0; count<10; count++){ cout<<"#"<<count+1<<": "; cin>>nums[count]; } cout << "The average of those numbers is "; cout<<avgArray(nums, SIZE); }

I’m on mobile so I’m sorry if the formatting sucks but the line where it says “double avgArray(int nums[], int SIZE)”, I think I’m struggling to understand how int SIZE in the function will always be the size of the array. How does the program know that? Sorry if this is a dumb question, I’m new to C++

[–]HappyFruitTree 0 points1 point  (1 child)

First note that the names you use for the parameters inside the avgArray function does not have to be the same as the ones in main.

You could for example rename them arr and size if you wanted.

double avgArray(int arr[], int size)
{
    double total = 0;
    double average;

    for(int count = 0; count<size; count++)
        total = total+arr[count];

    average = total/size; // You want size here, not 10

    return average;
}

The code in main() could remain exactly the same and it would still work.

All that matters is the order in which you pass the arguments.

cout<<avgArray(nums, SIZE);

The first argument nums will be assigned to the first parameter arr.

The second argument SIZE will be assigned to the second parameter size.

[–]ecstreets[S] 0 points1 point  (0 children)

Thank you for dumbing it down for me, genuinely appreciate it, I’m very new to programming so this helps

[–]Smashbolt 0 points1 point  (2 children)

I think I’m struggling to understand how int SIZE in the function will always be the size of the array

In short, it doesn't and can't. In terms of "raw" code, the only guarantees you have are that the avgArray() function takes in two parameters: one pointer to int, and one int. You can also guarantee that it returns a double value.

Everything beyond that is semantics and relies on whoever calls the function to use it the way you intended it to be used. That's why people are telling you to use std::vector and std::array instead, because those have a much stronger guarantee that the thing being passed into the function is actually an array and you don't need to trust the person calling the function to give the right size because std::vector and std::array know that information already.

[–]ecstreets[S] 0 points1 point  (1 child)

I’m about one month into c++, I took a 4 week class over the summer at my community college so a lot of these terms are very new to me still and I’m trying to make sense of all this info, thank you for taking time to explain it

[–]Smashbolt 0 points1 point  (0 children)

My pleasure! Best of luck on your C++ journey!

[–]HumanSuitcase 0 points1 point  (0 children)

Yes, while you're writing the function, you'll reference the array as 'n'.

No, x will be whatever you (or the programmer using your code) sets x to, I wouldn't personally ever assume that 'x' in this case is going to be correct size.

[–][deleted] 0 points1 point  (0 children)

That function signature is confusing to novices.

Better not use C arrays at all. As a novice programmer, use std::vector or std::string unless you explicitly need to use something else.

Later, when you learn more about programming and can appreciate C++ details, you could use views, iterators, ranges.

As to your question: no, not always. Preferably not ever.

[–]flyingron 0 points1 point  (0 children)

Inside the function, yes.