0

What is wrong with my code, everything looks fine but prints garbage. I just want to print array of elements entered by user.

Question is print 10 elements of an array entered by user. I know there are easier version of solving this problem, but I want to learn function return array technique. So, please don't suggest alternative solution, just try to fix this code. I don't see anything wrong because the address of a is passed onto m, and dereference m, to print array.

Question is print 10 elements of an array entered by user. I tried looking up videos on youtube searching function return array. I feel like scanf has something to do with it. I tried returning a but it will say function returns address of local variable... why? so I commented it. but why? it should return base address of an array right?

error:

none
enter 10 elements: 1 2 3 4 5 6 7 8 9 1
132731416707184220180041670672022018-4101503843276412416707184220180041670672022018-41015038432764123220180041670672022018-4101503843276412340041670672022018-4101503843276412345041670672022018-4101503843276412345641670672022018-41015038432764123456722018-4101503843276412345678-41015038432764123456789327641234567891

...Program finished with exit code 0
Press ENTER to exit console.
#include <stdio.h>

int main() {
  int a[10], i;

  printf("enter 10 elements: ");
  for (i = 0; i < 11; i++) {
    scanf("%d", &a[i]);

  }

  display(a, 10);
}

void display(int m[10], int n) {
  int i;
  for (i = 0; i < n + 1; i++) {
    printf("%d", *(m + 1));
  }
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
zappi
  • 15
  • 3
  • 1
    The array `a` have ten elements, with index from `0` to `9` (inclusive). Your loop `for(i = 0; i < 11; i++)` iterates over *eleven* elements. – Some programmer dude Jan 12 '23 at 10:39
  • 1
    Also, for any pointer or array `m` and index `i` the expression `*(m + i)` is exactly equal to `m[i]`. If we use it on `*(m + 1)` then that's the same as `m[1]` which is likely not what you want. – Some programmer dude Jan 12 '23 at 10:41
  • my apologies, I mentioned it below that I posted the unedited one, instead of new corrected code. I already knew that error but the problem still exists. Now you can see it but still there is an error – zappi Jan 12 '23 at 10:49
  • 1
    I reformatted the code snippet. That should make it much easier to see what's happening. Also, a minute in a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) should have told you the problem as well (much quicker than posting this question). So the lesson for today is: Please learn how to debug your programs. – Some programmer dude Jan 12 '23 at 10:59
  • @zappi Poor etiquette to change the question's code logic once answers arrive. Post rolled back and then reformatted. – chux - Reinstate Monica Jan 12 '23 at 12:16
  • my apologies for all this, when i used gdb, the problem was just existence of garbage values, i learned my lesson and problem is now solved, next time i will be very careful. – zappi Jan 12 '23 at 12:35

0 Answers0