1

the task I have is basically to create a main function with two other functions that basically input some numbers into malloc and other function that prints them. My problem is that the second function, print_pointer, doesn't seem to have the pointer in it. Thank you for your help :)

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

void create_malloc(int* p_p, int n)
{
    int i;

    p_p = (int*) malloc(n * sizeof(int));
    if (p_p == NULL)
    {
        printf("Memory not allocated.\n");
    }
    else
    {
        printf("Memory successfully allocated using malloc.\n");
    }
    for (i = 0; i < n; ++i)
    {
        int a;
        scanf("%d", &a);
        p_p[i] = a;
        /*p_p[i] = i*i; ///This is so I just don't have to input numbers all the time*/ 
    }
/*
    for (i = 0; i < n; ++i) ///This is supposed to be in a function by itself
    {
        printf("%d ", p_p[i]); 
    }
*/
}

void print_pointer(int* p_p, int n)
{
    int i;
    for (i = 0; i < n; ++i)
    {
        printf("%d", p_p[i]);
    }
}

int main()
{
    int* p = NULL, n;
    printf("Enter number of elements:");
    scanf("%d", &n);
    printf("Entered number of elements: %d\n", n);
    create_malloc(p, n);
    print_pointer(p, n);
    return 0;

}

I tried pretty much everything that I could with my very limited experience in c. I've just started studying it at UNI.

wictor33
  • 13
  • 3
  • 2
    *Return* the pointer instead! Passing a pointer to a function creates a *copy* of, so you assign the data `malloc`ed to this copy only, leaving the original pointer untouched. – Aconcagua Oct 25 '22 at 13:09
  • 1
    `create_malloc(p, n);` does not do anything for you. It passes the value of `p` to `create_malloc`. Then `create_malloc` has only the value. It knows nothing about the variable `p` in the `main` routine, and nothing that occurs in `create_malloc` changes the value of `p` in the `main` routine. You need `create_malloc` to either take a pointer to a pointer, so that it can use the pointer-to-a-pointer to set the pointer, or you need it to return a pointer, which the `main` routine will store in `p`. – Eric Postpischil Oct 25 '22 at 13:09
  • Never describe a problem as “doesn't seem to have the pointer in it.” Describe the observed behavior of the program and the desired behavior of the program. – Eric Postpischil Oct 25 '22 at 13:11
  • Note that you have a memory leak in any case, even if you correct your functions you need to `free` the string allocated at some point of time. – Aconcagua Oct 25 '22 at 13:11
  • About code style: You get more readable code if you limit the scope of the variable to as local as possible, in given case you could profit from declaring the loop variable in the loop header itself: `for(int i = 0; ...; ...)`. Just thing to think about a little: C++ committee considered the usefulness of so valuable that they even extended the syntax allowing to do the same for an `if` clause... – Aconcagua Oct 25 '22 at 13:14
  • This is a FAQ, please check the linked duplicate. – Lundin Oct 25 '22 at 13:39

0 Answers0