Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 8th, 2006, 7:25 PM   #1
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 4 Writlaus is on a distinguished road
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?
Writlaus is offline   Reply With Quote
Old Apr 8th, 2006, 7:36 PM   #2
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
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:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline   Reply With Quote
Old Apr 8th, 2006, 8:55 PM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 8th, 2006, 9:00 PM   #4
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
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:
Originally Posted by Mohamed Jihad
Durka durka!
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!
jayme is offline   Reply With Quote
Old Apr 9th, 2006, 12:07 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 9th, 2006, 12:08 AM   #6
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Use arrays when you're more concerned about efficiency.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 9th, 2006, 12:12 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Apr 9th, 2006, 1:01 AM   #8
Jessehk
The Oblivious One
 
Jessehk's Avatar
 
Join Date: May 2005
Location: Ontario, Canada
Posts: 648
Rep Power: 4 Jessehk is on a distinguished road
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!
Jessehk is offline   Reply With Quote
Old Apr 9th, 2006, 3:12 AM   #9
Writlaus
Hobbyist Programmer
 
Writlaus's Avatar
 
Join Date: Nov 2005
Posts: 149
Rep Power: 4 Writlaus is on a distinguished road
Hmm... returning a vector seems to work ok. I just use vector<int> func() {...} instead.
Writlaus is offline   Reply With Quote
Old Apr 9th, 2006, 8:49 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC