C语言指针,求大侠帮忙解释解释。

NowDoIT 2013-07-05 01:04:01
大概是这样的:
typedef struct a s_a;
typedef struct b s_b;

struct a {
char *s;
};

struct b {
s_a r;
s_a *p;
}

struct s_b *var;


我想通过ar访问a结构体中的s;
如果 var->p->s 我能够理解,因为都是指针嘛;

如果要访问 r 中的 s,该怎么访问?
var->r.s 还是 var.r->s 或者是 &var->r->s ?

谢谢大家!
...全文
474 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
NowDoIT 2013-08-12
  • 打赏
  • 举报
回复
谢谢楼上各位,尤其是 @cfjtaishan ,亲自写代码验证。 结贴散分,再次感谢!
一根烂笔头 2013-08-03
  • 打赏
  • 举报
回复
结构指针访问成员用-> 结构实例访问成员用.
fldmxp 2013-08-03
  • 打赏
  • 举报
回复
s是r的成员,s虽然是指针(s是不是指针没有关系)但是r并不是指针,所以是.而不是->
monkeydonkey 2013-07-06
  • 打赏
  • 举报
回复
仔细看看书本上->和.各自使用场景
monkeydonkey 2013-07-06
  • 打赏
  • 举报
回复
var->r.s
一根烂笔头 2013-07-06
  • 打赏
  • 举报
回复
LZ的变量名s_b好奇葩阿
tianfyt 2013-07-06
  • 打赏
  • 举报
回复
清_晨 2013-07-05
  • 打赏
  • 举报
回复
->是指针指向其成员的运算符 .是结构体的成员运算符 如:var->r.s="aaaaaa"; var是指针后面就接-> r是结构体(或者联合体)后面就接 . 如:var->p->s var是指针后面就接-> p还是指针后面就接->
X_Richard 2013-07-05
  • 打赏
  • 举报
回复
引用 11 楼 cfjtaishan 的回复:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct a s_a;
typedef struct b s_b;
 
struct a {
    char *s;
};
 
struct b {
    s_a r;
    s_a *p;
};
int main(int argc, char *argv[])
{
	//struct s_b *var;    //这种写法会让编译器报未定义指针类型的变量var
	s_b *var = (s_b*)malloc(sizeof(s_b));
	if (NULL == var)
	{
		fprintf(stderr, "malloc failed!\n");
		exit(EXIT_FAILURE);
	}
	var->r.s = (char *)malloc(16);
	if (NULL == var->r.s)
	{
		fprintf(stderr, "malloc failed!\n");
		exit(EXIT_FAILURE);
	}
	var->p = &var->r;    //指针用于指向结构体变量
	strcpy(var->r.s, "helloworld");
	fprintf(stdout, "%s\n", var->p->s);
	//puts(var->p->s);
	free(var->r.s);
	free(var);
	
	return 0;
}
代码中有不足之处,大家可以发表自己意见,直接指出来,我不胜感激。但讨论应该有一个前提,那就是拿出证据,能够说出一个123,让大家信服。
哈哈,你说那个不堪负载,没那么严重,我只是开玩笑的说一下编码风格,项目组不可能就40行代码,别那么认真。 BTW, 看来你释放内存后也没有好习惯把pointer set NULL啊,如果悬垂指针引起问题就不那么好找咯~~(真 正项目遇到后你就会注意了)。 个人建议,误见怪~~
majia2011 2013-07-05
  • 打赏
  • 举报
回复
可能是每个人的看法都不同吧,在我眼里,有括号反而更乱,呵呵呵
自信男孩 2013-07-05
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct a s_a;
typedef struct b s_b;
 
struct a {
    char *s;
};
 
struct b {
    s_a r;
    s_a *p;
};
int main(int argc, char *argv[])
{
	//struct s_b *var;    //这种写法会让编译器报未定义指针类型的变量var
	s_b *var = (s_b*)malloc(sizeof(s_b));
	if (NULL == var)
	{
		fprintf(stderr, "malloc failed!\n");
		exit(EXIT_FAILURE);
	}
	var->r.s = (char *)malloc(16);
	if (NULL == var->r.s)
	{
		fprintf(stderr, "malloc failed!\n");
		exit(EXIT_FAILURE);
	}
	var->p = &var->r;    //指针用于指向结构体变量
	strcpy(var->r.s, "helloworld");
	fprintf(stdout, "%s\n", var->p->s);
	//puts(var->p->s);
	free(var->r.s);
	free(var);
	
	return 0;
}
代码中有不足之处,大家可以发表自己意见,直接指出来,我不胜感激。但讨论应该有一个前提,那就是拿出证据,能够说出一个123,让大家信服。
自信男孩 2013-07-05
  • 打赏
  • 举报
回复
引用 9 楼 rent19870817 的回复:
[quote=引用 6 楼 cfjtaishan 的回复:]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct a s_a;
typedef struct b s_b;
 
struct a {
    char *s;
};
 
struct b {
    s_a r;
    s_a *p;
};
int main(int argc, char *argv[])
{
	//struct s_b *var;    //这种写法会让编译器报未定义指针类型的变量var
	s_b *var = (s_b*)malloc(sizeof(s_b));
	if (NULL == var)
	{
		fprintf(stderr, "malloc failed!\n");
		exit(EXIT_FAILURE);
	}
	var->r.s = (char *)malloc(16);
	if (NULL == var->r.s)
	{
		fprintf(stderr, "malloc failed!\n");
		exit(EXIT_FAILURE);
	}
	var->p = &var->r;    //指针用于指向结构体变量
	strcpy(var->r.s, "helloworld");
	fprintf(stdout, "%s\n", var->p->s);
	//puts(var->p->s);
	
	return 0;
}
我通过测试加不加括号是一样的,不过,加上括号能够让表达式更好理解。
都像你这么写代码,内存有多少都不够用的[/quote] 嗯,我承认没有free我申请的空间,这只是一个demo程序,程序退出后会自动释放我申请的空间,所以没有手动free掉。除了这方面,我看不出我申请了多少空间,而让内存“不堪负载”。
X_Richard 2013-07-05
  • 打赏
  • 举报
回复
引用 6 楼 cfjtaishan 的回复:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct a s_a;
typedef struct b s_b;
 
struct a {
    char *s;
};
 
struct b {
    s_a r;
    s_a *p;
};
int main(int argc, char *argv[])
{
	//struct s_b *var;    //这种写法会让编译器报未定义指针类型的变量var
	s_b *var = (s_b*)malloc(sizeof(s_b));
	if (NULL == var)
	{
		fprintf(stderr, "malloc failed!\n");
		exit(EXIT_FAILURE);
	}
	var->r.s = (char *)malloc(16);
	if (NULL == var->r.s)
	{
		fprintf(stderr, "malloc failed!\n");
		exit(EXIT_FAILURE);
	}
	var->p = &var->r;    //指针用于指向结构体变量
	strcpy(var->r.s, "helloworld");
	fprintf(stdout, "%s\n", var->p->s);
	//puts(var->p->s);
	
	return 0;
}
我通过测试加不加括号是一样的,不过,加上括号能够让表达式更好理解。
都像你这么写代码,内存有多少都不够用的
_胖胖 2013-07-05
  • 打赏
  • 举报
回复
是这样的: 结构体变量名.成员名; 指针变量名->成员名。 另外,指针变量名->成员名 在计算机内部会被转化成(*指针变量名).成员名的方式来执行。 所以你这个问题的结果是 var->r.s
自信男孩 2013-07-05
  • 打赏
  • 举报
回复
var->p = &var->r;    //指针用于指向结构体变量
这一句中,最好能加上括号。如下:
var->p = &(var->r);    //指针用于指向结构体变量
自信男孩 2013-07-05
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct a s_a;
typedef struct b s_b;
 
struct a {
    char *s;
};
 
struct b {
    s_a r;
    s_a *p;
};
int main(int argc, char *argv[])
{
	//struct s_b *var;    //这种写法会让编译器报未定义指针类型的变量var
	s_b *var = (s_b*)malloc(sizeof(s_b));
	if (NULL == var)
	{
		fprintf(stderr, "malloc failed!\n");
		exit(EXIT_FAILURE);
	}
	var->r.s = (char *)malloc(16);
	if (NULL == var->r.s)
	{
		fprintf(stderr, "malloc failed!\n");
		exit(EXIT_FAILURE);
	}
	var->p = &var->r;    //指针用于指向结构体变量
	strcpy(var->r.s, "helloworld");
	fprintf(stdout, "%s\n", var->p->s);
	//puts(var->p->s);
	
	return 0;
}
我通过测试加不加括号是一样的,不过,加上括号能够让表达式更好理解。
Regan-lin 2013-07-05
  • 打赏
  • 举报
回复
(var->r).s
水平不流 2013-07-05
  • 打赏
  • 举报
回复
楼上都解决了....
buyong 2013-07-05
  • 打赏
  • 举报
回复
(var->r).s
赵4老师 2013-07-05
  • 打赏
  • 举报
回复
(var->r).s
//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).
//
//Table 1.1   C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator         | Name or Meaning                         | Associativity |
//+------------------+-----------------------------------------+---------------+
//| ::               | Scope resolution                        | None          |
//| ::               | Global                                  | None          |
//| [ ]              | Array subscript                         | Left to right |
//| ( )              | Function call                           | Left to right |
//| ( )              | Conversion                              | None          |
//| .                | Member selection (object)               | Left to right |
//| ->               | Member selection (pointer)              | Left to right |
//| ++               | Postfix increment                       | None          |
//| --               | Postfix decrement                       | None          |
//| new              | Allocate object                         | None          |
//| delete           | Deallocate object                       | None          |
//| delete[ ]        | Deallocate object                       | None          |
//| ++               | Prefix increment                        | None          |
//| --               | Prefix decrement                        | None          |
//| *                | Dereference                             | None          |
//| &                | Address-of                              | None          |
//| +                | Unary plus                              | None          |
//| -                | Arithmetic negation (unary)             | None          |
//| !                | Logical NOT                             | None          |
//| ~                | Bitwise complement                      | None          |
//| sizeof           | Size of object                          | None          |
//| sizeof ( )       | Size of type                            | None          |
//| typeid( )        | type name                               | None          |
//| (type)           | Type cast (conversion)                  | Right to left |
//| const_cast       | Type cast (conversion)                  | None          |
//| dynamic_cast     | Type cast (conversion)                  | None          |
//| reinterpret_cast | Type cast (conversion)                  | None          |
//| static_cast      | Type cast (conversion)                  | None          |
//| .*               | Apply pointer to class member (objects) | Left to right |
//| ->*              | Dereference pointer to class member     | Left to right |
//| *                | Multiplication                          | Left to right |
//| /                | Division                                | Left to right |
//| %                | Remainder (modulus)                     | Left to right |
//| +                | Addition                                | Left to right |
//| -                | Subtraction                             | Left to right |
//| <<               | Left shift                              | Left to right |
//| >>               | Right shift                             | Left to right |
//| <                | Less than                               | Left to right |
//| >                | Greater than                            | Left to right |
//| <=               | Less than or equal to                   | Left to right |
//| >=               | Greater than or equal to                | Left to right |
//| ==               | Equality                                | Left to right |
//| !=               | Inequality                              | Left to right |
//| &                | Bitwise AND                             | Left to right |
//| ^                | Bitwise exclusive OR                    | Left to right |
//| |                | Bitwise OR                              | Left to right |
//| &&               | Logical AND                             | Left to right |
//| ||               | Logical OR                              | Left to right |
//| e1?e2:e3         | Conditional                             | Right to left |
//| =                | Assignment                              | Right to left |
//| *=               | Multiplication assignment               | Right to left |
//| /=               | Division assignment                     | Right to left |
//| %=               | Modulus assignment                      | Right to left |
//| +=               | Addition assignment                     | Right to left |
//| -=               | Subtraction assignment                  | Right to left |
//| <<=              | Left-shift assignment                   | Right to left |
//| >>=              | Right-shift assignment                  | Right to left |
//| &=               | Bitwise AND assignment                  | Right to left |
//| |=               | Bitwise inclusive OR assignment         | Right to left |
//| ^=               | Bitwise exclusive OR assignment         | Right to left |
//| ,                | Comma                                   | Left to right |
//+------------------+-----------------------------------------+---------------+
加载更多回复(1)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧