a program in c

By harshbedi ·
here i am writing the question
-initialise an array(integer) of 10 elements in main().
-pass the entire array to a function modify()
-in modify() multiply each element of array by 3
-return the control to main() and print the new array elements in main().

use pointers for solving the question......

the problem i am getting is in writing the code in modify() and how to return the entire array to the main()..

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Answers

Collapse -

Just pass the array

by Tony Hopkinson In reply to a program in c

int[] myArray[10}


void function Modify(int[] theArray)
//theArray is now a pointer to the first element in myArray.

int k;
for (k=0;k<10;k++)
theArray[k] = theArray[k] * 3;

C passes arrays by reference as a default.

If you want to or have to pass a pointer to modify

It Would be Modify(&myArray)

void Modify (int[] *theArray)
int i;
for (k=0;k<10;k++)
theArray[k] = theArray[k] * 3;

Normally you'd pass the dimensions of the array into modify as well especially with the pointer example as you could literally pass anything in there.

Call it after defining myArray[3]. Save your work first though, something bad could happen : D

Adding an extra int parameter Sizeof(myArray) \ SizeOf(int) would pass in ten for the upper limit on the loop.

One of the reasons why C is so powerful and C code is so bug ridden.

Related Discussions

Related Forums