-4

can you tell me why this doesn't work.

int function(int);

int main()
{
    int g[20],N;
    printf("Type N");
    scanf("%d",&N);
    g[20]=function(N);
    printf("s[0] is %d\n",g[0]);
    printf("s[1] is %d\n",g[1]);
    printf("s[2] is %d\n",g[2]);

}

int function(int N){
   int s[20];
s[0]=1;
s[1]=3;
s[2]=5;

return s[20];
}

I just want that my function return this numbers 1,3,5 but it returns some weird numbers, i thinks it's adresses or something. PS. I just began to learn C.

  • 4
    In an array of 20 elements last element has index **19**. `g[20]` is out of bounds! – Aditi Rawat Nov 08 '17 at 18:47
  • 1
    Writing to `g[20]` is [undefined behaviour](https://en.wikipedia.org/wiki/Undefined_behavior). Similar problem with `s[20]`. – P.P Nov 08 '17 at 18:48
  • 1
    `s` is just going to contain random values, whatever was in those memory addresses. Except for index 0,1,2 which have been assigned something. – BurnsBA Nov 08 '17 at 18:48
  • Also `s[20]` is *local* to`function()`. Hence your `g[]` is bound to have garbage values owing to the fact that you have not initialized the elements of array `g[]`. – Aditi Rawat Nov 08 '17 at 18:50
  • can you copy my code, make changes and send it back? – Jack Internacional Nov 08 '17 at 18:51
  • No, this is not a site where we do your work for you. Instead you need to take the time to read and understand the comments that are being given to you and then try to apply that in your code on your own. – takendarkk Nov 08 '17 at 18:52
  • like [this](https://ideone.com/CvUm3H) ? – BLUEPIXY Nov 08 '17 at 19:00
  • @BLUEPIXY just out of curiosity..what is the need of sending `int N` as an argument in the function? – Aditi Rawat Nov 08 '17 at 19:07
  • @AditiRawat It has not been used in posted code either. I guess that it probably represents the length of the sequence. Perhaps, I think that it will be changed in the future. – BLUEPIXY Nov 08 '17 at 19:12
  • @BLUEPIXY alright – Aditi Rawat Nov 08 '17 at 19:19

2 Answers2

0

You need to something like this.

void function(int *);

int main()
{
    int g[20],N;                  // array g local to main funciton
    printf("Type N");
    scanf("%d",&N);
    function(g);            // invoke function by passing array base address as an argument
    printf("s[0] is %d\n",g[0]);   // the first three positions of the g array
    printf("s[1] is %d\n",g[1]);  // have not been set by function
    printf("s[2] is %d\n",g[2]);  // they are also all unknown values

}

void function(int *s){ 
    s[0]=1;       //*(s+0)      
    s[1]=3;     
    s[2]=5;

}
Aditi Rawat
  • 784
  • 1
  • 12
  • 15
-2

From your code, you seem to have some misunderstanding about arrays and scope. The two arrays you declare start with unknown values until you set them. see my comments to your original code:

int function(int);

int main()
{
    int g[20],N;                  // array g local to main funciton
    printf("Type N");
    scanf("%d",&N);
    g[20]=function(N);            // invoke function with int typed in and return int
    printf("s[0] is %d\n",g[0])   // the first three positions of the g array
    printf("s[1] is %d\n",g[1]);  // have not been set by function
    printf("s[2] is %d\n",g[2]);  // they are also all unknown values

}

int function(int N){ // you never use N in this function, why is it a parameter
   int s[20];        // declare a int array local to function
s[0]=1;              // you only set the first three items in array, rest are unknown values.
s[1]=3;
s[2]=5;

return s[20]; // return the 20th int item of the array, (unknown memory contents)
}  
professor.jenkins
  • 155
  • 1
  • 1
  • 8
  • In an array of 20 elements **last element has index 19**. `g[20]` and `s[20]` is out of bounds!! – Aditi Rawat Nov 08 '17 at 18:59
  • Frankly speaking you need to read about (A) variable scope and (B) how to return more than just one value from a function(hint: you would be requiring pointers in your case). – Aditi Rawat Nov 08 '17 at 19:02