Solved

Array element as function parameter

Posted on 2012-04-11
4
491 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
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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to creat good software interface ? 1 60
word0 challenge 3 78
How To Loop - Python 19 79
Why isn't object file created? 6 42
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This article will show, step by step, how to integrate R code into a R Sweave document
This video teaches viewers about errors in exception handling.
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.

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now