The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 05-17-2008, 10:05 AM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 301
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
Passing variables into a single array in a function?

How would you pass variables into an array in a function?

For example: void Function(int Array[0, 1, 2, 3, 4])
then calling Function(0, 3, 7, 1, 3); would set Array[0] to 0, Array[1] to 3, etc...

I know that doesn't work because I tried it but how would you do something similar or do you have to put the variables into an array before hand and do: void Function(int Array[]);
Function(Array[5]);

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to MrPickle For This Useful Post:
vikramsony (05-24-2008)
  #2 (permalink)  
Old 05-19-2008, 10:22 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
What about

Code:
Function(int[] myArray);
that should work..

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to HelloWorld For This Useful Post:
vikramsony (05-24-2008)
  #3 (permalink)  
Old 05-19-2008, 03:47 PM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 301
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
What about

Code:
Function(int[] myArray);
that should work..
I know how to parse arrays, I wanted to know whether you could pass individual variables straight into an array.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to MrPickle For This Useful Post:
vikramsony (05-24-2008)
  #4 (permalink)  
Old 05-20-2008, 04:58 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
Quote:
Originally Posted by MrPickle View Post
I know how to parse arrays, I wanted to know whether you could pass individual variables straight into an array.
Ermm... I seriously don't get on what are you trying to do, you're trying to pass an individual variable straight into array? what does that mean? You're passing variable to element of an array? Remember, array is just collection of variables!

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 05-21-2008, 09:45 AM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 301
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
Quote:
Originally Posted by HelloWorld View Post
Ermm... I seriously don't get on what are you trying to do, you're trying to pass an individual variable straight into array? what does that mean? You're passing variable to element of an array? Remember, array is just collection of variables!
I want to pass the variables straight into an array in a function eg;

calling Function(1, 3, 2, 1, 0);
then they would be put straight into an array in Function's arguments.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to MrPickle For This Useful Post:
vikramsony (05-24-2008)
  #6 (permalink)  
Old 05-21-2008, 02:34 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
PT Admin
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,118
iTrader: (0)
HelloWorld is a jewel in the roughHelloWorld is a jewel in the roughHelloWorld is a jewel in the rough
something like..

Code:
Function (int a, int b, int c, int d, int e)
    x[0] = a;
    x[1] = b;
    x[2] = c; // ... and so on
???

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by HelloWorld : 05-21-2008 at 02:36 PM.
Reply With Quote
The Following User Says Thank You to HelloWorld For This Useful Post:
vikramsony (05-24-2008)
  #7 (permalink)  
Old 05-21-2008, 05:32 PM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
Its still not entirely clear to me what exactly you want to do - Are you trying for array initialisation?
Code:
int my_arr[] = { 1, 2, 3, 4, 5, 6, 7 };

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Bench For This Useful Post:
vikramsony (05-24-2008)
  #8 (permalink)  
Old 05-22-2008, 08:58 AM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 301
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
It's hard to explain. I want to know if it's possible to put pass variables, straight into an array that is the argument of a function. Argh, that doesn't explain it.

If I declared a function like:
Code:
void MyFunction(int MyArray[]){}
Could you pass individual variables straight into that array without assigned them to an array before passing them to the function.

Maybe this explains things:
Code:
#include <iostream>
using namespace std;

void MyFunction(int MyArray[]){
   cout << MyArray[0] << endl;
   cout << MyArray[1] << endl;
   cout << MyArray[2] << endl;
}

int Main(){
   MyFunction({1, 4, 6}); //Something or another
   return 0;
}
Output:
Quote:
1
4
6

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to MrPickle For This Useful Post:
vikramsony (05-24-2008)
  #9 (permalink)  
Old 05-22-2008, 09:57 AM
Bench Bench is offline
Full Programmer
Join Date: Jul 2007
Location: UK
Posts: 113
iTrader: (0)
Bench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished roadBench is on a distinguished road
No, that's not possible, since a function will only accept an array parameter by way of a pointer-to its first element. a pointer is an address in memory, which implies that an array must already exist in memory before you may pass it to a function.

Perhaps you could give a broader view of the problem you're trying to solve, and someone might be able to suggest how to deal with it.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following 2 Users Say Thank You to Bench For This Useful Post:
MrPickle (05-22-2008), vikramsony (05-24-2008)
  #10 (permalink)  
Old 05-22-2008, 03:27 PM
MrPickle's Avatar
MrPickle MrPickle is offline
Sr. Programmer
Join Date: Nov 2007
Location: England, Lincolnshire
Posts: 301
iTrader: (0)
MrPickle is on a distinguished roadMrPickle is on a distinguished roadMrPickle is on a distinguished road
It wasn't a problem, I was just curious to as how you could do it if it's possible. My assumption was that you'd already have to have an array defined.

Thanks.

__________________
PM me and tell me your name or a phrase and I shall write it in Elf for you. English Tengwar to be precise.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to MrPickle For This Useful Post:
vikramsony (05-24-2008)
Reply


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 03:21 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50