C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 10th, 2003, 04:17 AM
dodervang dodervang is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 53 dodervang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 28 sec
Reputation Power: 11
Send/return array

C++

I want to send an array to a function, do something with it inside, and then return it. Then later send it to another function and return it and so on.
How do I do this? Itīs easy with a regular int or something, but I canīt get it to work with arrays. The array looks like this int f[26].

Can you please tell me how to declare the array, and how to send/return it to/from functions? And how to declare the functions (if itīs any different from regular int functions)

I guess I donīt have to tell you that Iīm pretty new to this

Reply With Quote
  #2  
Old March 10th, 2003, 09:37 AM
balance balance is offline
.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Posts: 296 balance User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 11
i don't know c++ but i think it'll be the same situation in c, which is like this:

you never pass a whole array to a function as such - it's impossible. what you actually do is pass a pointer to the array. even if you see code that appears to be passing an array to a function, it's in fact passing a pointer. what this means is that when you alter the array (via a pointer to it), the array gets effected directly, externally to that function - so *no need for any return in the function*, which is pretty handy. even if you don't understand pointers it doesn't matter too much in this particular case as you can make use of them fairly easily almost without realising you're using them.

so bearing in mind your array is f[26] you'd define the function like:
Code:
void function(int f[])	/* a pointer to f is being passed rather than the array itself */
{
	f[3]=12;	/* treat it as usual array (although in fact you're using pointers) */
			/* and no need for return - the array has been changed directly */
}

and you call that function like:
Code:
function(f);

so the array name needs passing (which actually behaves as a pointer to the array) to the function.

it maybe a little different in c++. not sure.

Last edited by balance : March 10th, 2003 at 09:40 AM.

Reply With Quote
  #3  
Old March 10th, 2003, 09:42 AM
dodervang dodervang is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 53 dodervang User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 m 28 sec
Reputation Power: 11
Thank you!

Your code worked perfectly, so I guess that it isnīt different from C in this particular thing.
Thanks!

Reply With Quote
  #4  
Old March 10th, 2003, 12:36 PM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
Quote:
Originally posted by balance
it maybe a little different in c++. not sure.
I am fairly certain that this is the same way you would do it in C++. It adds nothing or take nothing away from the way you would do it in C.

There is another method of doing this, though, which is legal in both C and C++:

Since you are essentially passing a pointer into the function, you can actually accept it as a pointer, and it will still work. Ex: Instead of doing this:
void function(int f[])
you can do this:
void function(int *f)
and the following code will still work:
f[3]=12;

In fact, the above array accessing code can be changed to pointer notation, as well. So, instead of doing this:
f[3]=12;
you can do this:
*(f+3)=12;
AND, this will work with either of the above two function headers. So, arrays and pointers are really interchangable.

Reply With Quote
  #5  
Old March 12th, 2003, 10:01 PM
andy3109's Avatar
andy3109 andy3109 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 421 andy3109 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 15 h 46 m 21 sec
Reputation Power: 11
Send a message via AIM to andy3109
How can you make a void function when you are setting an array in the parameters? What is the point of a parameter if nothing is returned to it? I don't understand.

-andy
__________________
hmmm...

Reply With Quote
  #6  
Old March 13th, 2003, 06:21 AM
Jason Doucette's Avatar
Jason Doucette Jason Doucette is offline
jasondoucette.com
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 378 Jason Doucette User rank is Private First Class (20 - 50 Reputation Level)Jason Doucette User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 7 h 23 m 8 sec
Reputation Power: 11
Question 1: How can you make a void function when you are setting an array in the parameters?

place void as the return type of the function to make it void. The fact that you are accessing an array that is passed in as one of the parameters (which you may or may not change) has absolutely nothing to do with the return value of the function. Of course, you can 'return' more than just the return value of the function if you pass in parameters that are pointers to global variables (or references to global vars), as you can change these values (by dereferencing the pointer, or simply storing a value in the reference), which change their values outside of the function, as well (i.e. any changes made are made directly to the global vars, not the copies inside the function). Passing in an array is the same thing as passing in a pointer to an array, and therefore this global array's elements can be changed. Therefore, in a sense you are 'returning' more than just the return value of the function.

Question 2: What is the point of a parameter if nothing is returned to it?

Parameters are not only meant for the sole purpose of returning values to the calling mechanism. Take, for instance, a function called sqr, whose purpose is to return the square of the value passed in. You would declare it as so:
double sqr(double x)
{
return(x*x);
}

This does not pass back any value through x (in fact, if x was changed within the function, it would not change the parameter you passed in - only the copy of that parameter is changed, and its value is forever lost after the function terminates). To return a value, we use the function's return value. It would not make any sense to have this function change the value of x (as a reference or a pointer to some global variable), since you couldn't use the function like this:
printf("2*2 = ",sqr(2));
and, if you couldn't do this, either:
printf("x*x + y*y = ",sqr(x)+sqr(y));
if you didn't want the values of x or y to change after this print statement is run.

I hope this clears things up.

Last edited by Jason Doucette : March 13th, 2003 at 06:23 AM.

Reply With Quote
  #7  
Old March 15th, 2003, 07:46 AM
draelon draelon is offline
Dissident
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2003
Location: New York
Posts: 1,671 draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level)draelon User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 8 h 8 m 13 sec
Reputation Power: 48
Pointers can be a confusing topic, so allow me to add my two cents...

Normally when you pass a variable into a function, you are creating a copy of that variable for the function to work on. If you change the value of the variable in the function, it does not affect the original value.

When you use pointers, instead of making a copy of the variable, you are actually telling the function where the original variable resides in memory space and allowing the function to act directly on the original. Therefore, if you make any changes within the function, the orginal variable will be changed as well. That's why you don't need a return value if you are passing pointers as parameters.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Send/return array

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap