It also happens if you’re trying to access an instance when you have a pointer, and vice versa:
struct foo { int x, y, z; }; struct foo a, *b = &a; b.x = 12; /* This will generate the error, should be b->x or (*b).x */
As pointed out in a comment, this can be made excruciating if someone goes and typedef
s a pointer, i.e. includes the *
in a typedef, like so:
typedef struct foo* Foo;
Because then you get code that looks like it’s dealing with instances, when in fact it’s dealing with pointers:
Foo a_foo = get_a_brand_new_foo(); a_foo->field = FANTASTIC_VALUE;
Note how the above looks as if it should be written a_foo.field
, but that would fail since Foo
is a pointer to struct. I strongly recommend against typedef
:ed pointers in C. Pointers are important, don’t hide your asterisks. Let them shine.
Related Posts:
- What does “request for member ‘*******’ in something not a structure or union” mean?
- Difference between a Structure and a Union
- Warning: assignment from incompatible pointer type
- How to properly malloc for array of struct in C
- Initializing array of structures
- Structure padding and packing
- Why and when to use static structures in C programming?
- error: struct has no member named X
- Constructor for structs in C
- Why should we typedef a struct so often in C?
- Why should we typedef a struct so often in C?
- dereferencing pointer to incomplete type
- “error: assignment to expression with array type error” when I assign a struct field (C)
- How do you make an array of structs in C?
- Map like structure in C: use int and struct to determine a value
- How to initialize a struct in accordance with C programming language standards
- Incompatible implicit declaration of built-in function ‘malloc’
- What is the cause of flexible array member not at end of struct error?
- waitpid, wnohang, wuntraced. How do I use these
- How to initialize array to 0 in C?
- C: error: expected ‘)’ before ‘;’ token
- make: Nothing to be done for `all’
- Why am I getting this error: “data definition has no type or storage class”?
- Data argument not used by format strings in C
- execv vs execvp, why just one of them require the exact file’s path?
- difference between
and - Setting std=c99 flag in GCC
- Warning/error “function declaration isn’t a prototype”
- Returning an array using C
- warning: initializer element is not computable at load time
- How to prevent multiple definitions in C?
- error: ‘for’ loop initial declarations are only allowed in C99 mode [duplicate]
- How can you print multiple variables inside a string using printf?
- c – warning: implicit declaration of function ‘printf’
- Flushing buffers in C
- What are 0x01 and 0x80 representative of in C bitwise operations?
- How to make parent wait for all child processes to finish?
- error: aggregate value used where an integer was expected
- Undefined Reference issues using Semaphores
- What is time(NULL) in C?
- Use of cudamalloc(). Why the double pointer?
- C dynamically growing array
- Why should we check WIFEXITED after wait in order to kill child processes in Linux system call?
- OpenGL — GL_LINE_LOOP —
- How to use EOF to run through a text file in C?
- Incorrect checksum for freed object on malloc
- Valgrind complains with “Invalid write of size 8”
- Scanning Multiple inputs from one line using scanf
- Expression must be a pointer to a complete object type using simple pointer arithmetic
- Segmentation fault (core dumped) due to fgets – I think
- C subscripted value is neither array nor pointer nor vector when assigning an array element value
- Unknown ending signal when using debugger gdb
- Valgrind: invalid read of size 4 -> sigsegv, works fine without valgrind and in visual studio
- error: too few arguments to function `printDay’ (C language)
- Is there a good Valgrind substitute for Windows?
- Difference between sizeof(char) and sizeof(char *)
- Difference between “move” and “li” in MIPS assembly language
- How to empty a char array?
- Assembly x86 – “leave” Instruction
- Undefined reference to main – collect2: ld returned 1 exit status
- How can one see content of stack with GDB?
- Is there a way to have printf() properly print out an array (of floats, say)?
- Proper way to empty a C-String
- malloc(): memory corruption
- How do you allow spaces to be entered using scanf?
- Excess elements of scalar initializer for pointer to array of ints
- How does one represent the empty char?
- Can I define a function inside a C structure?
- Where is the
header file on Linux? Why can’t I find ? - What is the difference between %g and %f in C?
- Why is the sizeof(int) == sizeof(long)?
- C Unknown type name ‘my_structure’
- “warning: useless storage class specifier in empty declaration” in struct
- How to allocate array of pointers for strings by malloc in C?
- Reading in double values with scanf in c
- “-bash: gcc: command not found” using cygwin when compiling c?
- How to format strings using printf() to get equal length in the output
- “-bash: gcc: command not found” using cygwin when compiling c?
- In C, what exactly happens when you pass a NULL pointer to strcmp()?
- lseek/write suddenly returns -1 with errno = 9 (Bad file descriptor)
- What are the differences between if, else, and else if?
- Should I use printf(“\n”) or putchar(‘\n’) to print a newline in C?
- What do \t and \b do?
- strtok segmentation fault
- C Error: declaration shadows a local variable — Won’t let me repeatedly replace the value of my float variable
- MIPS to C Translation
- write() to stdout and printf output not interleaved?
- Pointer to 2D arrays in C
- Pointer to a string in C?
- Cross Platform C library for GUI Apps?
- Implicit function declarations in C
- Pre increment vs Post increment in array
- Convert long long to string in C?
- Using pointer to char array, values in that array can be accessed?
- How can I create a dynamically sized array of structs?
- how to stop a loop arduino
- How to normalize a mantissa
- Level vs Edge Trigger Network Event Mechanisms
- How to use shared memory with Linux in C
- When is it ok to use a global variable in C?