c++ - why this binary conversion does not work? -


#include<stdio.h> #include<conio.h>  unsigned * bin(unsigned n) {     unsigned a[16];     int = 0, j = 0;     (i = 0; < 16; i++) {         a[i] = n & 0x1;         n = n >> 1;     }     return a; }  void main() {     unsigned n = 5;     int = 0;     unsigned * = bin(n);     (i = 15; >= 0; i--) {         printf("%d\n", (*(a + i)));     }     getch(); } 

please binary conversion not work. i'm trying calculate x^n using binary comversion. can anybode help??

you returning pointer local variable. variable stored on stack, , not valid after function returns.

dereferencing pointer lead undefined behavior.

the solution either make variable static, or pass in array argument function, or (as noted in comment james kanze) use type copies contents.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -