Home > Software design >  Is there a way to deference a struct as a pointer in C?
Is there a way to deference a struct as a pointer in C?

Time:02-17

Purely experimental

Before we jump in, this is purely experimental and I don't care about undefined behaviours.

Using clang 13.

My code

typedef struct {
    char *chr;
    int64_t len;
} string;

string str;
 
int main() {
    str.chr = "ABCDE";
    str.len = 6;

    printf ("str.chr  = %p\n", str.chr);
    printf ("str      = %c\n", *str);
}

Output

str.chr  = 0x5c0845c56e // same address
str      = 0x5c0845c56e // same address

I want to do something like *str. This'd be equivalent to *(0x5c0845c56e). So str is basically used as a pointer to the string.

Ofc, the compiler throws an error at that

error: indirection requires pointer operand ('string' invalid)
   printf ("*str      = %c\n", *str);
                               ^~~~

I've tried casting str to void * and char * but that doesn't work.

CodePudding user response:

Same address but not same type.

  • *str means value of the struct, ie all its contents.
  • *(str.chr) means character pointed by str.chr.

CodePudding user response:

I want to do something like *str. This'd be equivalent to *(0x5c0845c56e). So str is basically used as a pointer to the string.

But str is not a pointer. It is a structure. To answer the titular question, no, there is no way in C to dereference a value of structure type.

Ofc, the compiler throws an error at that

error: indirection requires pointer operand ('string' invalid)
   printf ("*str      = %c\n", *str);
                               ^~~~

As I said, you cannot dereference a structure.

I've tried casting str to void * and char * but that doesn't work.

Of course not. C does not define any conversions between structures and pointers.

I guess you must be trying to draw an analogy with arrays, but their behaviors simply are not analogous. However, if you want the first member of your structure then you can use the & operator to obtain the structure's address, convert it to a pointer to the type of the first member, and dereference that to get an lvalue representing the first member:

    printf ("str      = %c\n", *(*(char **)&str));
    // here ---------------------^^^^^^^^^^^^^^

But inasmuch as that depends on knowing the structure type's definition (to get the pointer conversion correct), it provides no advantage whatever over accessing the first member in the standard way:

    printf ("str      = %c\n", *str.chr);

CodePudding user response:

str is not declared as a pointer so you cannot dereference it using *.

#include <stdio.h>
#include <stdint.h>

typedef struct {
    char *chr;
    int64_t len;
  } string;

  string str;

  int main () {

    int s = sizeof(char *);
    str.chr = "ABCDE";
    str.len = 6;

    printf ("str.chr  = %llu\n", &str.chr);  //address of str.chr
    printf ("char *      = %llu\n", &str);  //first 8 bytes of str structure
 }

output

@home:~$ ./a.out
str.chr  = 94437003264064
char *      = 94437003264064
  • Related