Multiplying array elements by 3. C program on Xcode

Discussion in 'Mac Programming' started by Mugambo, Jul 25, 2012.

  1. macrumors 6502


    Jul 4, 2009
    On the last return line, I get the error:
    Thread 1: Signal SIGABRT.

    I have written the C program on xcode.
    This is a program to multiply an array by 3 and return new values.
    Please tell me what the error is and any suggestions to make this program work.

    Learning C from 2 weeks.

    #include <stdio.h>

    int modify(int anth[10])
    int i;
    return anth[10];

    int main(int argc, const char * argv[])
    int i, *anth;
    int arr[10]={1,2,3,4,5,6,7,8,9,10};
    printf("%d.", arr);
    return 0;
  2. macrumors 68040


    Jan 10, 2005
    Dallas, TX
    After the initial declaration of arr, you no longer need the [10]. You do need [] in the declaration of modify. Right now, instead of passing the address of the 0th element of arr to modify, you're passing the address of the (non-existant/overflow) 10th element to modify. If you just pass arr you should be fine. Also, there's no real reason to return an int from modify, you're not using it. Right now when you call modify it is mutilating memory situated right after arr, which will cause all sorts of trouble. Try to make these changes and see how it goes.

  3. thread starter macrumors 6502


    Jul 4, 2009
    Thanks Lee!
    1) I removed 10(overflow) within [] from modify() while defining the function.

    2) while calling function modify. Used modify(arr) instead of modify(&arr[10])

    And it worked perfect!

    While returning values however, we need the [10]. Is it because we're indicating size of array being returned?
  4. macrumors G5


    Nov 25, 2005
    The array element arr [10] doesn't exist. If you try to return something that doesn't exist, you are just asking for trouble. There is no reason for your function to return anything.

Share This Page