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
Solved

Array element as function parameter

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

856 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