0
#include <stdio.h>
#include <stdlib.h>

typedef struct{
    int lab;
    int arr[20];
    int vac;
}bookings;

void checkIn(bookings *in)
{
    printf("It is %i",in[0].arr[0]);
}

int main(){
    bookings b[5];
    int a,c;
    /*Initializing struct variables*/
    for(a=0;a<5;a++)
    {
        for(c=0;c<20;c++)
        {
            b[a].arr[c]=0;
            b[a].vac=20;
            /*printf("Lab %i PC %i is %i\n",a,c,b[a].arr[c]);*/
        }
        /*printf("Lab %i has vacant %i\n",a,b[a].vac);*/
    }
    b[0].lab=101;
    b[1].lab=201;
    b[2].lab=301;
    b[3].lab=401;
    b[4].lab=501;
    checkIn(&b);

    return 0;
}

Based on the code, I've assigned values of 0 into the arr[20] array and in each struct, the vac value will be 20. What I'm trying to do is to pass the array of structs into the function and read the value in the function. According to what I've read so far, using the pointers in the function parameters and passing the array of struct address should work.

When compiled, it displays the error,'error passing argument1 of checkIn from incompatible pointer type'. My expected output is

It is 0

Any help would be quite appreciated!

7
  • Could you explain the difference of your question to this one? stackoverflow.com/questions/35614862/… I am not saying that it is a duplicate, but maybe it is close enough to be helpful. Commented May 4, 2018 at 6:00
  • Either checkIn(b) or checkIn(&b[0]) should work. Commented May 4, 2018 at 6:01
  • 2
    Should be checkIn(b) not address of b, you're currently technically passing in a pointer to a pointer to the first element Commented May 4, 2018 at 6:03
  • @Jose Fernando Lopez Fernandez so the struct passed into the function is already an address in itself? Sorry, I'm still fresh with pointers and structs. Commented May 4, 2018 at 6:07
  • @cswannabe only because it's an array. Otherwise the struct would be passed by value Commented May 4, 2018 at 6:08

3 Answers 3

4

The expression &b gives the address of the b array, not the first element. They are at the same location in memory, but the type is different. &b has type bookings (*)[5]; that is, a pointer to an array of five bookingses. Your function wants just bookings* (pointer to either a single bookings or the start of an array of an unknown number of them). You can get this with array-to-pointer decay by skipping the & and just passing b, or by looking at &(b[0]), the address of the first element of the array.

Sign up to request clarification or add additional context in comments.

Comments

3

You need to pass the array without & - checkIn(b);

When there is & the array doesn't convert to a pointer to the first element and the type is bookings (*)[5].The function expect bookings* and you get a warning.

Without & the array decays to a pointer and the expected type is correct.

2 Comments

Why shouldn't I use & when passing the struct into the function?
@cswannabe the array name represents the address of the first element, similar to how the name of a function represents the function address
2

It should be:

checkIn(b);

&b would return type of bookings(*)[5]. It would be useful if you need to modify your array (not its elements, they already passed by reference).

11 Comments

The types bookings** and bookings (*)[5] are different and incompatible, and &b is the latter. The first requires the actual memory location of b[0] to be stored in a different variable, and there is no other variable to store it in (b just has the actual elements, not a pointer).
(Also, the parentheses are important; bookings*[5] is an array of five pointers to bookings, and that can be converted to bookings**.)
@Swift how bookings(*)[5] will be converted to bookings**?
That is odd; I can get to it just fine. Can you see Compiler Explorer not through my link? The code I used is int double_deref(int** p) { return **p; } int main() { int arr[5] = {1, 2, 3, 4, 5}; return double_deref(&arr); } (although with more useful whitespace than will be preserved in the comment), the language was set to C, and I compiled with gcc 7.3 and the options -Wall -Wextra -pedantic -O3. The warning options were unnecessary; the -O3 helps to see what is actually happening without an excess of assembly to wade through.
I don't actually see any double pointers or pointers-to-array in sendmsg or struct msghdr, although I haven't looked at the message part of the sockets API enough to be sure I haven't missed something.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.