Understanding how strict aliases are grouped

I am trying to understand how structural aliases are compiled into a binary, especially when there is a modifier in front of it. I notice that having an alias, with or without *, leads to different binaries (checked with shasum). For example, given the following structure:

typedef struct __foobar {
    int a;
    int b;
} *pfoobar_t, foobar_t;

how the following variable declarations differ from the C standard and the compiler perspective:

const pfoobar_t my_var;
const foobar_t *my_var;
foobar_t const *my_var;

Thanks in advance.

+4
source share
2 answers

Difference between

const pfoobar_t my_var;

and

const foobar_t *my_var;

lies in the fact that the first declares my_varitself as const, while the second declares itself a my_varpointer to an object const.

, , const ( ). NULL; , NULL.

, , . , struct __foobar, .

.

 foobar_t *const my_var;

, , (, struct __foobar), ( ) .

+3

- . , int a; int b; 32- 8- (2 x 4 ).

(, 9 ), (, ) (12 , sub $esp, 12).

, - , , const:

:

typedef struct __foobar {
    int a;
    int b;
} *pfoobar_t, foobar_t;

:

#include <stdio.h>

int main(){
  foobar_t var;
  const pfoobar_t my_var = &var;
  my_var->a = 3;
  printf("%d\n", my_var->a);
}

. :

#include <stdio.h>

int main(){
  foobar_t var;
  const foobar_t *my_var;
  my_var->a = 3;
  printf("%d\n", my_var->a);
}

:

const.c: In function ‘main’:
const.c:13:3: error: assignment of member ‘a’ in read-only object
   my_var->a = 3;

, , , ELF.

+1

Source: https://habr.com/ru/post/1681498/


All Articles