Topic:   passing arrays to function - error   (Read 4238 times)


0 Members and 1 Guest are viewing this topic.

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
    • View Profile
passing arrays to function - error
« on: May 09, 2013, 09:17:50 PM »
Code: [Select]
#include <iostream>
#include <stdlib.h>

int * coins;
int * buffer;

int sumofarray(int* array) {
int sum;
int i  = (sizeof(array)/sizeof(array[0]));
std::cout << i << "\n";

return sum;
}

int main (int argc, char * const argv[]) {
   
int coins[] = {7, 1, 2, 8};
std::cout << (sizeof(coins)/sizeof(coins[0])) << "\n";
std::cout << sumofarray(coins);
}

so theres a dynamic array with four used cells. In main() the equation to determine how many elements it has works correctly returning 4, but when it's executed in the function (in which the same array has been passed to) it incorrectly returns 2.
If anybody has any insight they could give to help me with this that'd be greatly appreciated.
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
    • View Profile
Re: passing arrays to function - error
« Reply #1 on: May 09, 2013, 09:43:31 PM »
I... Thought I would try to interpret the syntax but I'm thoroughly confused here, sorry Kurt. >_>

PS - it says this was Kurt's post #666
PPS - that's odd, I'm at 1777 o-o
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4408
    • View Profile
Re: passing arrays to function - error
« Reply #2 on: May 10, 2013, 12:12:16 AM »
c/c++ doesn't store the size of the array. sizeof(array) returns the size of the pointer, not the size of the array.

Basically sizeof calculates the size of the datatype. Not what's in the data type. So you gotta remember how large your array is when you make it. Of course if you want a dynamic array you can use a vector.

I'm sort of guessing what you're trying to do but this might work:
Code: [Select]
#include <iostream>
#include <stdlib.h>
using namespace std;

int * coins;

int sumofarray(int* array, int len) {
int sum;
for (int i = 0; i < len; i++) {
sum += array[i];
}

return sum;
}

int main (int argc, char * const argv[]) {
   
int coins[] = {7, 1, 2, 8};
cout << "Number of elements: 4" << endl;
cout << sumofarray(coins, 4);
}

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
    • View Profile
Re: passing arrays to function - error
« Reply #3 on: May 10, 2013, 12:19:14 AM »
Okay, well, that makes a lot more sense to me and I just have to ask...
How did you come up with

int i  = (sizeof(array)/sizeof(array[0]));
« Last Edit: May 10, 2013, 12:21:21 AM by Connors »
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
    • View Profile
Re: passing arrays to function - error
« Reply #4 on: May 10, 2013, 11:49:33 PM »
@Connors It's something I used all the time with calloc() and malloc() in my C days when I remembered how to use pointers. It's the size of the array in bytes divided by the size of the data type of the first (zeroth) spot of the array in bytes, yeilding the number of elements in the array.
@Gan I need a way to send a dynamic array of indeterminant length to the sumofarray() function and get it to test the length inside of there, and then use that in the for() loop; like what you did but a variable representing the length instead of the number 4
Note: in an unshown part of the main() the array is stored in a buffer reallocated and then reallocated with an arbitrary size.
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4408
    • View Profile
Re: passing arrays to function - error
« Reply #5 on: May 11, 2013, 03:23:40 AM »
If you don't know the length of an array, there isn't a way to get the length.

Circuit


  • GMG-er

  • **


  • Posts: 299

  • blast from the past
    • View Profile
Re: passing arrays to function - error
« Reply #6 on: May 13, 2013, 08:20:10 PM »
Hey Kurt.  Gan mentioned earlier that you can use a vector instead of an array.  Vectors are easier to use because vectors can automatically resize themselves.  Another thing about vectors is that the vector class has a size() method which can be called from anywhere that the vector is passed to.  This means that when you pass a vector, you don't need to pass the size along with it, because the method can determine the size of the vector by calling size().  You just have to make sure to pass the vector by reference or as a pointer, because if you don't, it will automatically be passed by value.

I'm curious, why do you want the function to determine the size of the array?  Why not pass the size of the array as a parameter?
« Last Edit: May 13, 2013, 08:23:08 PM by Circuit »

GMG Kurt


  • GMG-er

  • **


  • Posts: 682

  • Sorry for being such a noob
    • View Profile
Re: passing arrays to function - error
« Reply #7 on: May 15, 2013, 09:32:29 PM »
Gan was right I couldn't find the size of the array outside of the main() because sizeof() is a runtime function. Currently I'm going to keep everything inside of the main() instead of passing the arrays to the function, and will deffinetly look into vectors cause they sound interesting.
@ circuit I wanted to just pass the array because in this program I wanted an array I could keep deallocating and reallocating depending on the number of elements that I needed. While tracking the size and giving that as a parameter is definitely an option (and an easier alternative) I just felt like doing it this way to do it, wanted the challenge.
Just your average Weekend Warrior.
Yes I know I have bad spelling, it's what makes me such a good programmer!

"Old art, weather magnificent or wretched, is always the raw material of new art. The artist's job, though, is to

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4408
    • View Profile
Re: passing arrays to function - error
« Reply #8 on: May 16, 2013, 01:44:56 AM »
If you want a challenge, make your own array class that you can initialize with a certain amount of variables that keeps track of it's size.
You'll then be able to pass it by pointer or reference.