I have this code:
1 #include <stdio.h>
2 #include <string.h>
3 #define LENGTH(a) sizeof(a)/sizeof(a[0]);
4
5 int *getNbits(int, int, char *);
6
7 int main() {
8 char ins[] = "00001111000011110000111100001111";
9 int *x = getNbits(0, 32, ins);
10
11 for (int i = 0; i < LENGTH(x) ; i++){
12
13 printf("%d", *(x + i));
14 }
15 return 0;
16 }
17
18 int* getNbits(int start, int offset, char instr[33]) {
19
20 int result[offset - 1];
21 for (int i = 0; i < offset; i++) {
22 result[i] = instr[i + start] == '1' ? 1 : 0; //- '0';
23 }
24 return result;
25 }
Basically, getNbits() gets an array of chars (which are 0's or 1's) and returns an array of ints, each element is a 0 or a 1.
If I try to print the array "result" as I am creating it in the for loop (using printf("%d",result[i]) ) it will return 000011110000.....
But I am having trouble with the return type, it gives me a warning: function returns address of local variable. And the for loop in main just prints garbage values.