I've just begun learning C code and I've been given an exercise to create the two functions you can see in my code, createData()
and udskriv()
where createData
should make an array using user input, and the udskriv
method should print it using a pointer.
However my code only gives me 1 or 2 of the right numbers and then usually a 0 and one very high number (like 10 digits) when printed. I was able to make it work by implementing it without functions by just running the loops in the main
method, but I can't get it to work with them.
I have some experience with Java which might help you explain why I'm doing something wrong.
#include <stdio.h>
static int* createData()
{
int test[4];
int c;
int *ptr;
printf("Indtast 4 tal, 1 af gangen");
for (c = 0; c < 4; c++)
{
scanf("%d", &test[c]);
}
ptr=&test;
return (ptr);
}
static void udskriv(int* ptr)
{
int i;
for (i=0;i<4;i++)
{
printf("%d\n",*ptr++);
}
}
int main(void)
{
udskriv(createData());
}