Thread: Passing array back to main function

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    23

    Passing array back to main function

    Hello people i'm having problems with my program, what its supposed to do is ask the user how many integers to store, then for every integer it asks the user to store a number at the integer location, for example:

    User enters 3 integers to input
    - User enters number 4 and is stored in the first integer location
    - User enters number 78 and is stored in the second integer location
    - User enters number 12 and is stored in the third integer location

    Then the program displays the users integers in thier spectified locations example:
    4 in integer 1
    78 in integer 2
    12 in integer 3

    However after i put my integers in the program goes crazy and generates a crazy amount of integers before the program seg faults. and i'm not quite sure to to correct this error, any ideas would be helpful.

    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    
    void getInts(int * integersArray, int numInput);
    
    int main() {
    
      int * integersArray;
      int numInput;
      int i;
    
      getInts(integersArray, numInput);
    
      for (i = 0; i < numInput; i++) {
        printf("%d ",integersArray[i]);
      }
      
      return 0;
    }
    
    
    void getInts(int * integersArray, int numInput) {
      int i;
    
      printf("Please enter the number of integers you want to input\n");
      scanf("%d", &numInput);
    
      integersArray = (int *) malloc(sizeof(int) * numInput);
    
      for (i = 0; i < numInput; i++) {
        printf("please enter integer %d: ", i+1);
        scanf("%d", &(integersArray[i]));
      }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Things are passed by value in C, so the value of integersArray cannot be changed by the getInts function, despite your attempt to do so with the line
    Code:
    integersArray = (int *) malloc(sizeof(int) * numInput);
    Also the value of numInput cannot be changed, despite the scanf line.

    If you want those values to change, you must pass the address of those variables to your function instead.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You reassign integersArray inside getInts, so it no longer refers to the value submitted from main:
    Code:
    void getInts(int * integersArray, int numInput) {
      int i;
    
      printf("Please enter the number of integers you want to input\n");
      scanf("%d", &numInput);
    
      integersArray = (int *) malloc(sizeof(int) * numInput);
    That's the same as doing this:
    Code:
    #include <stdio.h>
    
    void test(int x) {
    	x = 42;
    }
    
    int main(int argc, const char *argv[]) {
    	int n = 666;
    	test(n);
    	printf("%d\n",n);
    
    	return 0;
    }
    "n" still equals 666. If you wanted to modify the value at the address of n in test (so that it equalled 42 in main), you'd do this:
    Code:
    test(int *x);  // prototype
    test(&n);  // call
    A parallel to what you are doing is:
    Code:
    void getInts(int ** integersArray, int numInput);
    getInts(&integersArray, numInput);
    "integersArray" is still a pointer, so this is called a pointer to a pointer. Then inside the function, you dereference:
    Code:
    *integersArray = (int *) malloc(sizeof(int) * numInput);
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    well i tried the above code, but this time i get an infinite loop of zeros if i enter 1 integer and store number 1 into the integer, however if i select 2 integers and put numbers within them i get a seg fault?, lol too confusing for me

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    84
    I feel you might need a pointer to a pointer here in the function prototype...

    Thus, have a pointer, pointing, to the address of the 1-D array, hence, something like:
    Code:
    getInts(int *(*integersArray), int numInput)
    though idk, im in the same boat and working on it right now :-/

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    
    void getInts(int *integersArray, int numInput);
    
    int main() {
    
      int * integersArray;
      int numInput;
      int i;
    
      printf("Please enter the number of integers you want to input\n");
      scanf("%d", &numInput);
    
    /* If you get the numInput here, then you can malloc() your array, before
    you call getInts(). That gets it all set with a good address. 
    */
    
    
      integersArray = (int *) malloc(sizeof(int) * numInput);
      getInts(integersArray, numInput);
    
    /* when the program returns, the address you received before from malloc(), is still 
    OK.
    
    The cast to int* is not necessary, but is not a compiler error.
    */
    
      for (i = 0; i < numInput; i++) {
        printf("%d ",integersArray[i]);
      }
      free(integersArray);
      getch();
      return 0;
    }
    
    
    void getInts(int *integersArray, int numInput) {
      int i;
    
      for (i = 0; i < numInput; i++) {
        printf("please enter integer %d: ", i+1);
        scanf("%d", &(integersArray[i]));
      }
    }

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    @ adak your above solution does not even output anything, hmm i tried messing with the program, but still i seg fault, at one point it ran an infinte loop. when it was generating the integers

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by greg677 View Post
    @ adak your above solution does not even output anything, hmm i tried messing with the program, but still i seg fault, at one point it ran an infinte loop. when it was generating the integers
    Why not post the code you currently have?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by greg677 View Post
    @ adak your above solution does not even output anything, hmm i tried messing with the program, but still i seg fault, at one point it ran an infinte loop. when it was generating the integers
    Change

    getch();

    to

    getchar();

    in the code.

    Then, put another getchar(); right after your scanf()'s

    (yes, every one of them).

    That will pull the remaining newline off, and if you accidentally input a char, it will stop it from looping forever, if the code is inside a loop .

    Of course, I ran this code several times before I posted it, and it ran flawlessly - but getch() works for me, automatically.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    23
    Like the following?

    Code:
    int main()
    {
       int *integersArray;
       int numInput;
       int i;
    
       getInts(integersArray, numInput);
    
       for (i = 0; i < numInput; i++)
       {
          printf("%d ",integersArray[i]);
       }
       free(integersArray);
       getchar();
       return 0;
    }
    
    void getInts(int *integersArray, int numInput)
    {
       int i;
    
       printf("Please enter the number of integers you want to input\n");
       scanf("%d", &numInput);
       getchar();
    
       integersArray = (int *) malloc(sizeof(int) * numInput);
    
       for (i =0; i < numInput; i++)
       {
          printf("Please enter an integer %d: ", i+1);
          scanf("%d", &(integersArray[i]));
          getchar();
       }
    }
    if that is correct it still does not work for me i am using a program called PUTTY to compile this, this may be the problem.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That looks right - did you remember to keep the #include files and the function prototype?

    I can't help you with PUTTY. Never used it. Why not use one of the highly regarded compilers/IDE's ?
    Last edited by Adak; 05-01-2010 at 07:31 AM.

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Putty isn't a compiler.

    But in any event, this:
    Code:
    getInts(integersArray, numInput);
    just hurts. integersArray can not not not not not NOT be changed by passing it to a function. If you want it to change, you must pass a "reference" to it, i.e., it's address:
    Code:
    getInts(&integersArray, numInput);
    And of course make the appropriate changes throughout getInts.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Replies: 6
    Last Post: 02-15-2005, 11:20 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM