Pass Array as a function parameter

Asked

Viewed 7,212 times

4

Make a program in C that reads a value x, dynamically create a vector of x elements and pass this vector to a function that will read the elements of this vector. Then, in the main program, the filled vector must be printed. Also, before finishing the program, you must release the allocated memory area. It follows what I did and what is asked in the exercise but I could not fit the function part. Done in DEV C ++ version 5.11.

#include<stdio.h>
#include<stdlib.h>

int main ()
{
int i,n;
printf ("Digite o numero de elementos que deseja \n ");
scanf ("%i",&n);
int *ptr;
    ptr =(int*) malloc (n * sizeof(int));
        int i; 
    for (i=0;i<n;i++)  /*queria passar essa parte do codigo para uma funçao ... essa parte do codigo faz a leitura dos valores do vetor
    {
        printf ("Digite os valores do vetor\n");
        scanf("%d",&ptr[i]);
    }   */
        for (i=0;i<n;i++)
        {
        printf ("%d",ptr[i]);
        }   
    free(ptr);
system("pause>null");
return 0;
}
  • And what’s your problem?

  • i would like to pass ptr, n to the function to do the reading of elements of my vector and then on main I would like to read the values that were typed in the function

  • Select the whole code and click {} to format everything.

  • thank you very much diego, I’m new around here

  • I’ll try one more time. What’s the problem with the code?

  • I’ll leave the code running and display the part I’d like to move to the function

  • 1

    bigown, the problem is that I can’t correctly pass the parameters to the function and can’t do with the main read the values entered in the function , why I didn’t know states right, I have this difficulty with function .

  • Your code is a little fuzzy and so is the question, I suggest that if you have a statement, put it in the question, so that we can see what’s being asked and compare it to what you did to see what’s missing. I also advise you, whenever you can try to indent the code, so that it is easier to see what is happening, helps in the visual reading of the code.

  • I’m sorry I posted the hurry and I didn’t care who would read the code, sorry I got into other topics and I saw that really difficult when the code is messy, I will improve in the next post and I will think more about the people who are trying to help

Show 4 more comments

1 answer

8


Just pass the array (pointer) and the number of elements for the function:

#include<stdio.h>
#include<stdlib.h>
void func(int *ptr, int x) {
    for (int i = 0; i < x; i++) {
        printf("Digite o valor %d do vetor\n", i + 1);
        scanf("%d", &ptr[i]);
    }
}
int main () {
    int x;
    printf("Digite o numero de elementos que deseja\n");
    scanf("%i", &x);
    int *ptr = malloc(x * sizeof(int));
    func(ptr, x);
    for (int i = 0; i < x; i++) {
        printf ("%d\n", ptr[i]);
    }   
    free(ptr);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Organized, coherent code, no redundancies, easier to read?

  • There’s no way I can give some like in your comment or something, I get really good

  • @ALFAEX as you can see this code is already indented, so it becomes easier to read for you and also for everyone who wants to help you.

  • 1

    What you can do is accept, as you have already done and when you have 15 points you can vote for anything on the site. See [tour].

  • i will improve and really bigown gave me a good example of how to act. Thank you so much I learned a lot from this post

Browser other questions tagged

You are not signed in. Login or sign up in order to post.