1
votes

I am trying to create a function that takes in a array of structs as a parameter. This is part of my code so far:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>
#define MAXREAD 256
#define MAXARR 64
struct block {
  char free;
  bool end;
};


void display(struct block *b){
    char *p;
    printf("|");
    for(int i=0; i<MAXARR; i++){
        p = &b[i].free;
        //printf("%c ", *p);
        printf("%c", (*p != '\0' ? '#':'-'));
        //printf("%d", p);
        if(b[i].end){
            //printf("%d\n", i);
            printf("|");
        }
        //printf("%c", blocks[i]->end ? "true":"false");
    }
    printf("\n");
}
int main(){
    char input[MAXREAD];
    struct block (*blocks)[MAXARR];
    char cmd;
    int number;
    blocks = malloc(sizeof(struct block));
    char *a;
    //int *m;
    for(int i=0; i<MAXARR; i++){
      blocks[i]->free = '\0';
      blocks[i]->end = malloc(sizeof(bool));
      blocks[i]->end = false;
    }
    blocks[MAXARR-1]->end = true;
    display(blocks);
    while(strcmp(input, "q") != 0){
        printf("How many blocks do you want to allocate/free?\n");
        fgets(input, MAXREAD, stdin);
        a = strchr(input, '\n');
        *a = '\0';
        sscanf(input, "%c %d",&cmd, &number);
        if(strchr(input, 'q')){
            break;
        } else if(strchr(input, 'a')){
            alloc(number, blocks);
        } else if(strchr(input, 'f')){
            dealloc(number, blocks);
        }
        display(blocks);
    }
    exit(0);
}

When I compile the program, this warning shows up:

warning: incompatible pointer types passing 'struct block (*)[64]' to parameter of type 'struct block *' [-Wincompatible-pointer-types] display(blocks);

I looked into these two posts and tried it out but the warning keeps showing up regardless:

Passing an array of structs in C

How to pass an array of struct using pointer in c/c++?

can someone please explain to me what is wrong with my program?

4
struct block (*blocks)[MAXARR];...hhmmmmSourav Ghosh
struct block (*blocks)[MAXARR]; this statement is understood as "blocks is a pointer to array of type struct block of MAXARR size".You need to change the signature of the display function to void display(struct block (*b)[]) or void display(struct block **b)nikhil mehta
You declare blocks as an array of 64 pointers to struct block. Then you use malloc to allocate a block of memory equal to the size of one struct block and assign this to blocks. This is wrong. blocks is already allocated to the stack and has the size of 64 pointers. You should instead allocate a block of memory for each block[i] inside the for loop. This is not your only problem though.axxis
1) always check the returned value from malloc(), sscanf(), alloc(), dealloc(), fgets(), strchr() to assure the operation was successful. 2) the contents of the first char of 'input' is being checked for 'q' before any specific contents are placed into 'input'. BTW: alloc() and dealloc() are prototyped in alloc.h, which is missing from the codeuser3629249
when #define'ing a numeric value always wrap that value in parens to avoid text-replacement errors. For readability by us humans, please insert a blank line between code blocks. The code is searching 'input' for 'q' and comparing the first char for 'q' suggest using the 'cmd' variable. which contains the first char of the read in line of data.user3629249

4 Answers

5
votes
struct block (*blocks)[MAXARR];

So, blocks is a pointer to an array of block's.

Then you called display() like :

display(blocks);

where, you defined display() as

void display(struct block *b)

which takes a block pointer only.

According to how you implement display(), here's some suggestion for you:

  1. Change void display(struct block *b) to void display(struct block *b[]) to print/access all elements blocks.

  2. Instead of . you need to use -> operator as b is a pointer to block.

  3. In main(), declare blocks as block *blocks[MAXARR]; which means an array of block pointers. Read this for more information.

  4. blocks = malloc(sizeof(struct block)); makes no sense. You need to malloc every block elements individually. Something like this:

    int i; for(i = 0; i< MAXARR; i++) { blocks[i] = (block *)malloc(sizeof(block)); }

  5. You do not need to blocks[i]->end = malloc(sizeof(bool));.

After all these, you can call display(blocks);.

2
votes
struct block (*blocks)[MAXARR];

This is causing problem as it creates a pointer to array of struct block. Whereas your function requires a pointer to struct block.

void display(struct block *b);

Therefore,it gives error.

struct block *b;

Will be correct statement.

0
votes

Define this:

struct block * blocks;

instead of struct block (*blocks)[MAXARR].

-1
votes

I think the explicit type-casting to struct pointer is needed while assigning memory dynamically. blocks = (struct block *) malloc(sizeof(struct block));