![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 4
![]() |
Quick question about arrays and functions
Just a simple question: What is the syntax for writing a function that returns an array? I thought it was something like...
int[] myFunc() {...}But I don't think that's the right way. Could someone write a simple example please? |
|
|
|
|
|
#2 | |
|
Professional Programmer
|
I had the same question recently.
ill find the link EDIT: link I ended up just returning a pointer to the array, it was just as affective in the position I was in.
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
In C/C++ (unlike some more abstract languages) an array is not an object. It's a collection of objects. You can't return a collection that isn't wrapped in something unitary. That's why C++ introduces class objects like vectors, deques, etc.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 | |
|
Professional Programmer
|
I just want to clear this up, if you have the choice between using an array and a vector, would it be more wise to choose vectors? From what I've been told, I understand a vector is pretty much an array with more functions, and expandable. So, would arrays be necessary if you know how to use vectors?
__________________
▄▄▄▄ Quote:
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it. Download Code::Blocks now! ▄▄▄▄ |
|
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
If you're using C++, my opinion is: use C++. Only in rare circumstances will the performance hit outweigh the productivity. People use Python and similar all the time without worrying about that, so why not with C++? STL classes for vectors, deques, lists, maps, and so forth, are much easier to use (with just a little study and practice) than ordinary arrays, and relieve the programmer of having to worry so much about sizes (memory allocation) and so forth. A vector is a class that encapsulates an array-like entity and overloads the [] so that one may access it by index. There are other access methods, as well.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Use arrays when you're more concerned about efficiency.
__________________
PFO - My daily dose of technology. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Yeah, yeah, whom among you guys ever concerned him/herself with efficiency before? The so-called efficiency is expensive in terms of productivity, which equates to bucks in the real world. If ya gotta, ya gotta, but I don't see any signs of it being of concern to lebenty-zillion percent of the members here.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
The Oblivious One
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4
![]() |
I felt like writing some C code.
Just replace malloc with new and free with delete (and the included header files).
#include <stdio.h>
#include <malloc.h>
#define SIZE 10
int *constructArray(int value, int numberOfTimes);
int main(void) {
int *numbs = constructArray(3, 10);
int x;
for(x = 0; x < SIZE; x++)
printf("%2d%s element of array is %d.\n", x + 1, (x + 1) == 1 ? "st" : "th", numbs[x]);
free(numbs);
return 0;
}
int *constructArray(int value, int numberOfTimes) {
int *result = (int *)malloc(numberOfTimes * sizeof(int));
int x;
for(x = 0; x < numberOfTimes; x++)
*(result + x) = value;
return result;
}
__________________
Dr. Zoidberg: [ecstatic] I'm going to a movie... with FRIENDS! |
|
|
|
|
|
#9 |
|
Hobbyist Programmer
Join Date: Nov 2005
Posts: 149
Rep Power: 4
![]() |
Hmm... returning a vector seems to work ok. I just use vector<int> func() {...} instead.
|
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Excellent. Put those guys telling you 'C' on hold, tell 'em the staff is busy, and you'll get back to 'em when you can.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|