Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

How to pass an array from function to main?


  • Please log in to reply
5 replies to this topic

#1 altair05

altair05

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:03:40 PM

Posted 10 April 2011 - 09:45 PM

I have 2 functions currently. One is my main() and the other is my getData() function.

My main() function will simply call my getData function for the moment.
My getData() function will prompt the user to enter 20 integers and will store those numbers in an array of size 20.

My question is how do I pass the entire array from my getData() function to my main(). I have tried several ideas to no avail.

I would like to pass my array via pass by address if possible.

In addition, lines 20 - 26 are my DEBUG statements just to see that my array is accepting the incoming values which they are.

1 #include<stdio.h>
  2
  3 void getData();
  4
  5 int main()
  6 {
  7  getData();
  8
  9   return(0);
 10 }
 11
 12 void getData()
 13 {
 14   int dataArray[20];
 15   int counter = 0;
 16   for(counter = 0; counter < 20; counter++)
 17   {
 18     printf("Enter data #%d:", counter + 1);
 19     scanf("%d", &dataArray[counter]);
 20   }
 21
 22   int x = 0;
 23   for(x = 0; x < 20; x++)
 24   {
 25   printf("Array: %d\n", dataArray[x]);
 26   }
 27 }

Edited by altair05, 10 April 2011 - 09:47 PM.


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:40 PM

Posted 11 April 2011 - 07:26 AM

myArray = getData();

Then have your function return an array.

#3 Didier Stevens

Didier Stevens

  • BC Advisor
  • 2,716 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:10:40 PM

Posted 11 April 2011 - 08:21 AM

myArray = getData();

Then have your function return an array.


But make sure that the array you create in getData is not an auto array, i.e. that it is not stored on the stack.
Otherwise you will use an array in main that doesn't exist anymore.
This may explain the issues you experience.

Didier Stevens
http://blog.DidierStevens.com
http://DidierStevensLabs.com

SANS ISC Senior Handler
Microsoft MVP 2011-2016 Consumer Security, Windows Insider MVP 2016-2019
MVP_Horizontal_BlueOnly.png

 

If you send me messages, per Bleeping Computer's Forum policy, I will not engage in a conversation, but try to answer your question in the relevant forum post. If you don't want this, don't send me messages.

 

Stevens' law: "As an online security discussion grows longer, the probability of a reference to BadUSB approaches 1.0"


#4 altair05

altair05
  • Topic Starter

  • Members
  • 110 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:03:40 PM

Posted 11 April 2011 - 02:40 PM

So something that looks like this?

1 #include<stdio.h>
  2
  3 int getData();
  4
  5 int main()
  6 {
  7  myArray = getData();
  8
  9   return(0);
 10 }
 11
 12 int getData()
 13 {
 14   int dataArray[20];
 15   int counter = 0;
 16   for(counter = 0; counter < 20; counter++)
 17   {
 18     printf("Enter data #%d:", counter + 1);
 19     scanf("%d", &dataArray[counter]);
 20   }
 21
 22   int x = 0;
 23   for(x = 0; x < 20; x++)
 24   {
 25   printf("Array: %d\n", dataArray[x]);
 26   }
 27   
 28   return(dataArray[])
 27 }


#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:40 PM

Posted 11 April 2011 - 05:15 PM

Sort of. ;)

#6 master131

master131

  • Members
  • 366 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Melbourne, Australia
  • Local time:07:40 AM

Posted 11 April 2011 - 08:57 PM

Yeah, you can't return arrays just like that. :wink:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users