Solved

Array element as function parameter

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

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sum13 challenge 24 77
scoreUp challenge 14 48
bunnyEars challenge 6 67
changeXy challenge 13 58
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

746 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

11 Experts available now in Live!

Get 1:1 Help Now