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.