Solved

Array element as function parameter

Posted on 2012-04-11
4
505 Views
Last Modified: 2012-04-11
Hi all -
I'm learning C and wanted to pose a question.  Can you pass an array element to another function?   For instance I'm in a 'FOR Loop' and generating the array with random numbers.   I would like to perform different calculations on these elements as they generate.  Can I pass the element value to another function which would perform the calculations?   Or should I save that element to a variable and then calculate returning and re-assigning the variable value back to the array element?   Thanks for the assist!
0
Comment
Question by:mojeaux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 31

Accepted Solution

by:
farzanj earned 167 total points
ID: 37833561
Yes, sure you can pass arrays in C.

Here's a link
http://www-ee.eng.hawaii.edu/~tep/EE160/Book/chap7/section2.1.2.html
0
 
LVL 34

Assisted Solution

by:sarabande
sarabande earned 167 total points
ID: 37833634
you also can pass single array elements:
void f(int i)
{
    ...
}

void g()
{
    int arr[10] = { 0, 1, 2, 3, 4, 5, 6, 7 ,8 , 9 };
    f(arr[5]);
}

Open in new window


the integer at arr[5] was passed 'by value' to function f. 'by value' means, the function uses a copy of the integer element.

Sara
0
 
LVL 3

Assisted Solution

by:jcgd
jcgd earned 166 total points
ID: 37834838
void sum10(int *pv) {
   
    *pv = *pv + 10;

}


void main()
{
    int v[] = { 0, 1, 2, 3, 4};
    for(int x=0;x<5;x++)
     {    
         printf("Before v[%d]=%d",x,  v[x] );
         sum10(&v[x]);
         printf(" After v[%d]=%d\n",x,v[x]);
      }
}
/* Results:
 Before v[0] = 0 After v[0] = 10
  Before v[1] = 1 After v[1] = 11
.
.
.

*/
0
 

Author Closing Comment

by:mojeaux
ID: 37834926
Thank you... that answered my question.   I appreciate your help!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question