遇到的几个小问题

1.segment fault 段错误
可能原因:访问只读的内存地址

#include<stdio.h>
#include<string.h>
void main()
{
    char *ptr="test";
    strcpy(ptr,"TEST");
}

*ptr为指针,是只读的内存地址,不能赋值
应改为

char ptr[6]="test";

只读变量定义可用const修饰

2.c语言规定数组初始化时可以赋值,但初始化后赋值不合法

int a[5]="abcd";

是合理的

int a[5];
a[5]="abcd";

是不合理的
但是可以对单个进行赋值

int a[5];
a[1]='a';

3.结构体中->和.的区别
如果定义的结构体是指针,访问结构体时用->
如果定义的结构体是结构体变量,访问时用.

struct token
{
char a;
};

struct token *p访问成员时用p->a
struct token p访问成员时用p.a

posted @ 2018-09-03 20:29  anmui  阅读(166)  评论(1编辑  收藏  举报