-4

I am just learning C and have my program print out incorrect values but I don't know how to fix it therefore I'd like to have your help. Here is the codes . The test function stores integer numbers (from 0 to 20) in an array and return it. That array is copied to a new array (p[20]) in the main function and I try to print every element of that array out using for loop however the value is not correct, for example i = 0 then p[0] should be 0 but it shows -443987883 ,....

#include <stdio.h>

int test();

int main ()
{
    int p[20];
    strcpy(p , test);
    int i;
    int N = 20;

    for (i=0; i < N; i++) {
        printf ("%i,%i\n", i , p[i]);
    } 
}

int test (int i , char o[])
{
    int N = 20;
    for (i = 0; i < N; i++) {
        o[i] = i;
    }
    return o;
}
kaylum
  • 13,833
  • 2
  • 22
  • 31
  • 4
    You have alot of basic errors. Perhaps time to go back to review your text book before proceeding further. Some highlights: `strcpy(p , test);` What are you trying to do there? Why are you trying to copy a function into an `int` array? `return o`: Why are you returning a char array when the return type is declared to be an int? – kaylum Jun 08 '16 at 05:28
  • 1
    When you attempt this again would strongly recommend you turn up warning levels on your compiler, pay attention to what the warnings are and fix the code so that there are no warnings before posting your question. – kaylum Jun 08 '16 at 05:30

1 Answers1

0

Your code contains a lot of errors. I am going to point them out and explain to you how to fix it.

  • First of all, you should definitely forget the statement :

    strcpy(p , test);
    

    as by this you try to copy a function to an array! If you think that this statement calls test function and passes p as an argument to this, this is not true.

  • If you want another function, other than main, to fill your array, the best way would be to dynamically allocate a pointer to int and pass it by reference to the function. You can read more about passing by reference or by value here.

    Let's see how you can do this. First of all, define N as a constant value outside your program :

    #define N 20
    

    Then, your main function should be modified like this :

    int main ()
    {
        int *p;
        int i;
    
        //Dynamically allocate a pointer to ints
        p = malloc(N*sizeof(int));
        if (p == NULL)
            printf ("Error in allocating memory\n");
    
        //Now call your function  
        p = test(&p);              //pass the pointer by reference
        for (i = 0; i < N; i++)
            printf ("%d, %d\n", i , p[i]);
    }
    

    and finally your test function should be modified accordingly like this :

    int* test (int** p)
    {
            for (i = 0; i < N; i++)
                *p[i] = i;
            return *p;
    }
    

    So the prototype of your test function should change from :

    int test();
    

    to :

    int* test(int**); 
    
Community
  • 1
  • 1
Marievi
  • 4,951
  • 1
  • 16
  • 33