Question

  • Creator
    Topic
  • #2269616

    a program in c

    Locked

    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()..

All Answers

  • Author
    Replies
    • #2536141

      Clarifications

      by harshbedi ·

      In reply to a program in c

      Clarifications

    • #2536114

      Just pass the array

      by tony hopkinson ·

      In reply to a program in c

      int[] myArray[10}

      Modify(myArray)

      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)

      Then
      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.

Viewing 1 reply thread