-2

I'm trying to create an array which at each index i have a bit field i tried this code:

struct bitF {
    unsigned int x1:1;
           ...
    unsigned int xn:1;
}intBF;
typedef struct intBF *arr[];
int main(){
int i;
unsigned int *arr2[10]
for(i=0;i<sizeof(arr2)/sizeof(unsigned int);i++)
{   arr2[i] = malloc(sizeof(intBF));
    arr2[i] = (unsigned int *)(&intBF);
    arr[i].x5 = 3;}
return 0;}

but i get error from the compiler for the last code line. i want to be able to go to the array at index 'n' and to change the bits values (without using masks). the compiler error (although i don't think it's relevant) is:

error: expected identifier or '(' before '[' token

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Rami Hassan
  • 149
  • 12
  • If you get an error from your compiler, you should copy/paste that error into your question, so others can help you with it. You also have a `_` in your second to last line, which is not proper syntax. – nos Jul 08 '16 at 11:49
  • Well, you code does not make much sense indeed and is badly formatted. And a bitfield-`struct` is certainly not a good idea if you want to address single bits. As a general rule, never cast if you don't really know all its implications, accept them and (most important) really have to. Also see [ask]. Re the compiler error: I'd assume the compiler does better know here what is relevant and what not. It should tell you something that the compiler is certain your code is wrong. – too honest for this site Jul 08 '16 at 12:25
  • *the compiler error (although i don't think it's relevant)* Compiler errors are **always** relevant. All of them. – dbush Jul 08 '16 at 12:26
  • seriously.... i know that my code is terribly wrong... otherwise i wouldn't ask!!... i have an assignment in which i need to create something like assembler and to store the input data by bits... most of my program is working just fine, but to convert the data to bits (to coding each line from the input files to bits) :S:S not working for me and i need help, criticizing not helping me, so pleas i need alternative code. – Rami Hassan Jul 08 '16 at 13:05
  • the main question is what i need to do to be able to write such line: **arr[i].x=3;** which x is a bit fields – Rami Hassan Jul 08 '16 at 13:39

2 Answers2

1

more elegant solution :

#define SIZE 5
typedef struct {
    unsigned int x1:2;
       ...
    unsigned int xn:2;
}TYPE;
main()
{
    int i;
    unsigned int *p;
    TYPE *arrType = malloc(sizeof(TYPE)*SIZE);
    arrType[0].x2 = 1;
    arrType[1].x2 = 2;
    arrType[2].x2 = 3;
    for(i=0;i<SIZE;i++)
    {
        p = (unsigned int *)(&arrType[i]);
        printf("%d\n",*p);
    }
    free(arrType);
    return 0;
}

and the output (as expected) is:

4
8
12
0
0
Rami Hassan
  • 149
  • 12
  • As I understand it , while the sum of [x1+...+xn] size (in my case [2+...+2]) is less than the size of the bits of one unsigned integer, you will get only one integer, otherwise you will get more than one integer, and the **p** is a pointer to the first integer in the structure. – Rami Hassan Jul 08 '16 at 21:49
0

I found the solution by Trial and error :))),

#define SIZE 5
struct bitF {
    unsigned int x1:2;
       ...
    unsigned int xn:2;
}intbf[SIZE];
main()
{
    int i;
    unsigned int *p[SIZE];
    for(i=0;i<SIZE;i++) 
    {
        p[i] = malloc(sizeof(intbf[i]));
        p[i] = (unsigned int *)(&intbf[i]); 
    }
    intbf[0].x2 = 3;
    for(i=0;i<SIZE;i++)
        printf("%d\n",*p[i]);
    return 0;
}

and i got the output:

12
0
0
0
0
Rami Hassan
  • 149
  • 12