Solved

Array element as function parameter

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sumDigits  challenge 7 137
wordappend challenge 8 220
Arduino EDI - Programming language 3 114
Problem to picture file 20 69
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
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 …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

679 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