-2

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());
}
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
pssh
  • 11
  • 1
  • Which book are you reading? You can't learn C by trial and error; it's potentially misleading and dangerous to do so, and the fact that you've got this wrong by doing so ought to demonstrate that trial and error doesn't work. The fact that you noticed the problem was actually a good result; many times people don't notice problems when they're learning by trial and error. If you haven't already got a book, I can highly recommend K&R 2E for you. – autistic Feb 12 '16 at 18:23
  • btw he is right: _I have some experience with Java_ explains his fault: ___Do not use C like Java___! They use a ___completely___ different data handling! – LittleByBlue Feb 12 '16 at 18:48

3 Answers3

2

You are returning a pointer to local data. The variable test in the function createData is a local variable, with what is technically called "storage class auto". When the function returns, the memory that was used for test might be used for other purposes.

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
1

You cannot return a pointer to a local variable . The local variable gets destroyed when the function exits. Either:

  • pass in a buffer on the function call
  • malloc the array you are going to return (and the caller must free it)
pm100
  • 48,078
  • 23
  • 82
  • 145
0

Now it works. I've added a memory allocation for your variable (test) and I've modified the return line of the function createData. The reason that the initial code does not works is that (test) is a local variable and will be destroyed after exiting the function createData.

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

static int* createData() 
{

        int *test;

    int c;
    int *ptr;

    test = malloc(4*sizeof(int));

    printf("Indtast 4 tal, 1 af gangen\n");
    for (c = 0; c < 4; c++)
    {
        scanf("%d", &test[c]);     
    }

    ptr=test;       /* ptr=&test is not ok. ---> test is already the address you need ---> if you want, you can do this ptr=&test[0] and it's ok */
    return (ptr);
}

static void udskriv(int* ptr)
{
    int i;
    for (i=0;i<4;i++)
    {
        printf("%d\n",*ptr++);
    } 
}

int main(void)
{
    udskriv(createData());
    return 0;
}
MD XF
  • 7,860
  • 7
  • 40
  • 71