0

As you can see in this part of my code:

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

struct s_box
{
    int matnr;
    double vol;
    int pos[2];
};

int main ()
{
    int matnr2;
    double vol2;
    int pos2[2];

    typedef struct s_box *pt_box;
    pt_box *p,*q;

    p = (pt_box *)malloc(sizeof(pt_box));
    q = (pt_box *)malloc(sizeof(pt_box));

    printf("Write a value for: \nMaterialnr= ");
    scanf("%d",&matnr2);

    printf("\nVolumen = ");
    scanf("%fl",&vol2);

    printf("\nxPos =");
    scanf("%d",&pos2[0]);

    printf("\nyPos =");
    scanf("%d",&pos2[1]);

    printf("\nzPos =");
    scanf("%d",&pos2[2]);
    //on the next line is my question:
    p -> matnr =  matnr2; //Codeblox gives me the next "error:'*p' is a pointer; did you mean to use '->'?"
    p->vol = vol2;//same error
    p->pos = pos2[0],pos2[1],pos2[2]);//same error
    printf("MaterialNr = \t%d/nVolumen = \t%f m^3\nPosition = (%d,%d,%d)",p->matnr,p->vol,p->pos[0],p->pos[1],p->pos[2]);
    // my target is to print the data stored in *p on the screen.
}

So the question is about the error: "did you mean to use '->'?" I already use it and Codeblox does not accept it. So what do I have to change and print the data on the screen after entering it from the keyboard.

P.S. I started learning C one week ago.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Johnmar
  • 11
  • 2

1 Answers1

2

To address the specific error in your question (there are others in your code): the issue lies in these two lines:

    typedef struct s_box *pt_box;
    pt_box *p,*q;

Here, you have defined the type pt_box as a pointer to an s_box structure; so, when you declare pt_box *p then p is a pointer to a pointer! So, to use the -> operator on that variable, you would first need to remove one level of referencing; so, for example:

    (*p)->vol = vol2;

However, what you really want is not to have that 'second level' indirection, so just declare your p and q variables as of 'plain' pt_box type (already a structure pointer), like so:

    typedef struct s_box* pt_box;
    pt_box p, q; // p and q are now POINTERS to s_box structures
    p = malloc(sizeof(struct s_box)); // Must be the size of the structure, not the pointer!
    q = malloc(sizeof(struct s_box)); // See note below!

Further, you can't assign entire arrays with one = operator, as you are attempting here:

    p->pos = pos2[0], pos2[1], pos2[2]); // Wrong, even without the odd ")" character!

Instead, you need to assign each element separately:

    p->pos[0] = pos2[0];  p->pos[1] = pos2[1]; // See below on the `pos[2]` third element!!

Another point: please see this Q/A: Do I cast the result of malloc?. Also, a more 'shorthand' way of getting the correct size on the calls to malloc is to use the 'dereference' of the destination variable; so, instead of the 'clumsy' sizeof(struct s_box), you could use *p and *q, like this:

    p = malloc(sizeof(*p));
    q = malloc(sizeof(*q));

And one more error that I found: You can only store two values in the array pos[2]! To get the z position, you will need to have three elements, so make the member int pos[3] (as also with the pos2 variable).

Please feel free to ask for any further clarification and/or explanation.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83