小编use*_*048的帖子

C指针数学与结构

试着更好地学习指针数学我编写了这段代码.目的是增加指针投掷结构并打印它的成员.我知道如何打印它的成员更简单的方法,但我真的想知道我的指针数学是如何搞砸的.谢谢.

typedef struct{ 
  int num;
  int num2;
  char *string;
} astruct ;

int main (int argc, const char * argv[])
{
  astruct mystruct = { 1234, 4567,"aaaaaaa"};

  astruct *address;
  address = &mystruct;

  // this does print 1234
  printf("address 0x%x has value of:%i\n",address, *address); 
  address = address + sizeof(int);

  //this does NOT print 4567
  printf("address 0x%x has value of:%i\n",address, *address); 
  address = address + sizeof(int);

  //this crashes the program, I wanted to print aaaaaaaa
  printf("address 0x%x has value of:%s\n",address, **address); 

  return …
Run Code Online (Sandbox Code Playgroud)

c pointers structure

1
推荐指数
1
解决办法
4516
查看次数

标签 统计

c ×1

pointers ×1

structure ×1