Modify an single passed array element

This is a discussion on Modify an single passed array element within the C Programming forums, part of the General Programming Boards category; Title basicly says what wont work. Half of the below works, I pass the whole array to a function and ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Modify an single passed array element

    Title basicly says what wont work. Half of the below works, I pass the whole
    array to a function and it modifes and returns it as expected, but when I pass
    an idividual array element it display it oddly then returns the wrong value but
    the correct element if that makes sense.

    Output I get is: passed array element: 816 ( where is should say 8 )
    modified array element: 8

    I gather my problem is possibly how I am outputing the displayed array element.
    I get no compile errors, its a logical error I cant faddle out but its proberly
    somthing really simple

    Code:
    #include <stdio.h>
    
    #define ARRAY_SIZE 5
    
    /*function prototypes*/
    void changeArray ( int, int[] );
    void changeElement ( int );
    
    /*main function - begins program execution -----------------------------------*/
    int main ( void )
    {
       int i, j;
    
       /*create and display original array*/
       int data[ ARRAY_SIZE ] = { 1, 2, 3, 4, 5 };
    
       printf("Original array:\n\n");
    
       for ( i = 0; i < ARRAY_SIZE; i++ )
       {
          printf("%d ", data[ i ]);
       }
    
       /*pass array size and name to function*/
       changeArray ( ARRAY_SIZE, data );
    
       /*output modified array output*/
       printf("\n\nModified array\n\n");
    
       for ( j = 0; j < ARRAY_SIZE; j++ )
       {
          printf("%d ", data[ j ]);
       }
    
       /*output original array element*/
       printf("\n\nOrginal array element data[ 3 ]:\n\n");
    
       printf("%d", data[ 3 ]);
    
       /*pass element to function*/
       changeElement ( data[ 3 ] );
    
       /*output modified array element*/
       printf("\n\nModified array element data[ 3 [:\n\n");
    
       printf("%d", data[ 3 ]);
    
       getchar(); /*freeze console output window*/
    
       return 0; /*return value from int main*/
    }
    
    /*function to modify and return array*/
    void changeArray ( int size, int x[] )
    {
       int i;
    
       for ( i = 0; i < size; i++ )
       {
          x[ i ] *= 2;
       }
    }
    
    /*function ro modify and return array element*/
    void changeElement ( int e )
    {
       printf("%d", ( e *= 2 ));
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,431
    Looks okay to me. The problem is that the printf() call in changeElement() does not print a new line (or alternatively, the printf() in main() before the call to changeElement() does not print a new line), so it looks like 816 when the 8 and 16 are separate.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Thanks laserlight that solved the problem I also had one printf() call than I needed, but the output is fine now. I knew it was somthing simple. Thank you for your help
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    /*function ro modify and return array element*/
    void changeElement ( int e )
    {
       printf("%d", ( e *= 2 ));
    }
    This function doesn't modify anything, right? It's got a local parameter that gets modified locally, but the value that was passed in doesn't get changed.

    --
    Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count/display non-repeated element of array
    By azsquall in forum C++ Programming
    Replies: 15
    Last Post: 07-10-2008, 09:42 AM
  2. Replies: 4
    Last Post: 01-05-2008, 10:30 PM
  3. How to return the last element of an array?
    By BuezaWebDev in forum C Programming
    Replies: 20
    Last Post: 03-26-2005, 09:57 AM
  4. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM
  5. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21