Thread: Read inputs for an dynmaically allocated array

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    Read inputs for an dynmaically allocated array

    I want to read user's input for my array using pointers, allocated space, read elements and return pointer.

    my function is called read:

    Code:
    double read *read_vector (int n) // n is the size of array, which user will enter
    
    int *vec = (int *) malloc (sizeof(int)*n);
    int i;
    for (i=0; i<10; i++)
                vec[i] = n;
    return vec;
    
    }
    but this code is not working, giving me segmentation fault, any help please?

    this is part of the main function i am using to read it:


    Code:
    int n;
      double *vector;
      /* Vector */
      printf("Vector\n");
      printf("Enter number of entries: ");
      scanf("%d",&n);
      printf("Enter %d reals: ",n);
      vector = read(n);

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Your *vec variable should be a double pointer since that is what you are returning and your for loop should probably iterate from i to n, not 10. At the moment your function isn't actually reading anything it just initializes the array with n, but I guess you know that.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    how do I read the inputs in?

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    changed some things:
    Code:
    double *read_vector(int n){
    double *vec = (double *) malloc (sizeof(double)*n);
    int i;
    for (i=0; i<n; i++)
           vec[i] = n;
           return vec;
    }
    


    now how do i store the users inputs?

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    this is my print vector function:

    Code:
    /* Print a vector with n entries pointed to by vec. */
    void print_vector(int n, double *vec){
    int i;
    for (i=0; i<n; i++) {
         printf("%d", vec[i]);
    }
    
    


    is this correct?

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    my read_vector is not even letting me enter the numbers for some reason.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Lina_inverse View Post
    my read_vector is not even letting me enter the numbers for some reason.
    Because you don't read any numbers. You just assign "n" to each member of "vec".

    For reading user input you probably want to use scanf().

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read inputs separated by space
    By acpower in forum C Programming
    Replies: 2
    Last Post: 04-10-2012, 09:54 AM
  2. Replies: 5
    Last Post: 10-23-2010, 05:09 AM
  3. How to handle inputs if the user inputs the wrong thing
    By bassist11 in forum C Programming
    Replies: 5
    Last Post: 09-22-2010, 04:28 AM
  4. stoping inputs into an array
    By thestrapman in forum C Programming
    Replies: 3
    Last Post: 08-19-2008, 04:41 PM
  5. trouble with creating a loop to read certain number of inputs
    By import tuner650 in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2008, 07:28 PM