简体   繁体   English

如何从仅具有该特定字段指针的字段的偏移量计算结构的起始地址

[英]How to calculate the start address of a structure from offset of a field with only that specific field pointer

I have following code.我有以下代码。 structure emp_ has five elements and all of its first four members could be printed with print_emp_details() function which takes a pointer to a emp_ struct.结构 emp_ 有五个元素,它的所有前四个成员都可以用print_emp_details() function 打印,它接受一个指向emp_结构的指针。 Given only a offset and a pointer to a particular field, start_address(glthread_node_t *node_t) should return (after casting) the start address of the struct emp_ which is then pass into print_emp_details() .只给定一个偏移量和一个指向特定字段的指针, start_address(glthread_node_t *node_t)应该返回(在转换之后)结构 emp_ 的起始地址,然后将其传递给print_emp_details() I am getting gibberish printf output from print_emp_details() and couldn't find what I am doing wrong.我从 print_emp_details() 得到乱码 printf output 并且找不到我做错了什么。 The offsetValue is only thing I am getting right. offsetValue 是我唯一正确的事情。

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include "glthread.h"


#define glthread_node_init(glnode)  \
    glnode->left = NULL;            \
    glnode->right = NULL;


#define offset(struct_name, fld_name) \
  (unsigned int)&(((struct_name *)0)->fld_name)

typedef struct glthread_node_ {
    struct glthread_node_ *left;
    struct glthread_node_ *right;
} glthread_node_t; 

typedef struct glthread_ {
    glthread_node_t *head;
    unsigned int offset;
} glthread_t;



typedef struct emp_ {
    char name[30];
    unsigned int salary;
    char designation[30];
    unsigned int emp_id;
    glthread_node_t glnode;
} emp_t;


struct emp_ *
start_address(glthread_node_t *node_t){
  unsigned int offsetValue;
  offsetValue=offset(emp_t,glnode);
  printf("offset = %d\n", offsetValue);
  return (struct emp_*)(node_t-offsetValue);
}

void
print_emp_details(emp_t *emp){

    printf("Employee name = %s\n", emp->name);
    printf("salary = %u\n", emp->salary);
    printf("designation = %s\n", emp->designation);
    printf("emp_id = %u\n", emp->emp_id);
}

void
init_glthread(glthread_t *glthread, unsigned int offset){

    glthread->head = NULL;
    glthread->offset = offset;
}

int
main(int argc, char **argv){
  emp_t *emp1=calloc(1,sizeof(emp_t));
  strncpy(emp1->name,"Harris", strlen("Harris"));
  emp1->salary= 100;
  strncpy(emp1->designation,"HR",strlen("HR"));
  emp1->emp_id=13;
  glthread_node_init((&emp1->glnode));
  print_emp_details(start_address(&emp1->glnode));
  return 0;
}

Output: Output:

offset = 72
Employee name = ?T??
salary = 0
designation = 
emp_id = 1920169263

The problem is at the end of start_address :问题出现在start_address的末尾:

return (struct emp_*)(node_t-offsetValue);

When you add or subtract a value from a pointer, it results in a pointer increased/decreated by that value times the size of the type the pointer points to .当您从指针中添加或减去一个值时,它会导致指针增加/减少该值乘以指针指向的类型的大小 It is done this way to make array indexing work.这样做是为了使数组索引工作。

You need to cast the pointer to char * to add/subtract single byte values:您需要将指针转换为char *以添加/减去单字节值:

return (struct emp_*)((char *)node_t-offsetValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM