汉克.docx
- 文档编号:8262967
- 上传时间:2023-01-30
- 格式:DOCX
- 页数:21
- 大小:37.45KB
汉克.docx
《汉克.docx》由会员分享,可在线阅读,更多相关《汉克.docx(21页珍藏版)》请在冰豆网上搜索。
汉克
chara[]="helloworld1"和char*p="helloworld2";的区别
分类:
C/C++2011-10-1517:
26 1053人阅读 评论
(1) 收藏 举报
平台c
1. 1 #include
2.
3. 2
4.
5. 3 int main()
6.
7. 4 {
8.
9. 5 char a[] = "hello world1";
10.
11. 6 char *p = "hello world2";
12.
13. 7 *(a+1)='q';
14.
15. 8 *(p+1)='q';
16.
17. 9 printf("%s\n",a);
18.
19.10 printf("%s\n",p);
20.
21.11
22.
23.12 }
运行结果
段错误。
注释 *(p+1)='q';
运行结果
hqllo world1
hello world2
原因:
一、a与p类型不同:
p为指向字符串的指针;a为保存字符串的数组。
5chara[]="helloworld1"; 是个赋初值的字符数组。
6char*p="helloworld2"; 是个字符串常量指针;
指针变量p在栈里面
字符串常量"helloworld2"在全局数据区,数据段,只读,不可写
二、"helloworld2"和"helloworld1"字符串保存的位置不同。
"helloworld1"保存在栈
中,可用*(a+1)='q'修改,"helloworld2"保存在全局数据
区,位置是在.rodata中,不能修改*(p+1)='q'
三、只有2中情况下,数组名不可等同视为数组指针,&与sizeof操作符
“&”的区别:
&p取得是指针变量的地址,而非字符串"hello world2"的首地址,p指向字符串的首地址;&a取得
就是字符串"hello"的首地址。
“siziof”的区别:
下面假设在ia32平台上运行,保存变量的内存空间不同。
sizeof(p); // return 4, pointer size
sizeof(a); // return 13, array size
1.5 char a[] = "hello world1";
2.(gdb) p a
3.$1 = "\000<\006�\005�?
�"//空
4.(gdb) s
5.6 char *p = "hello world2";
6.(gdb) p a
7.$2 = "hello world1"
8.(gdb) p &a
9.$3 = (char (*)[13]) 0xbfde0583 //在栈
10.(gdb) s
11.7 *(a+1)='q';
12.(gdb) p p
13.$4 = 0x80484c0 "hello world2" //在数据段
14.(gdb) p &p
15.$5 = (char **) 0xbfde0590 //在栈
在函数里charp[]="helloworld"与char*p="Helloworld"造成的效果不一样,为什么?
请高人告知!
2006-11-0919:
02lee072 | 分类:
其他编程语言 | 浏览2252次
请看两个程序:
(1)
char*GetString(void)
{
charp[]="Helloworld";
returnp;
}
voidmain()
{
char*str=NULL;
str=GetString();
cout< } (2) char*GetString(void) { char*p="Helloworld"; returnp; } voidmain() { char*str=NULL; str=GetString(); cout< } 为什么 (1)输出的是乱码, (2)能够输出helloworld 我觉得可能是指针字符串和数组字符串分配空间时的区别,有谁知道具体的吗? 谢谢! 分享到: 2006-11-0919: 22提问者采纳 这个我知道.因为我也为这个问题困扰过,做过一些探讨. "Helloworld"作为静态字符串实际上存储在数据区,但写程序的人不知道这个地址,而程序本身知道。 当某一函数以 {charp[]="Helloworld";...} 方式使用此静态字符串时,实际上相当于: charp[12]; strcpy(p,"Helloworld"); .... p[12]是在栈里临时分配的。 虽然p指向的内容是"Helloworld",但是这是复制品,不是原件。 当函数结束,charp[]就被程序回收了,所以p[]的内容就不再是"Helloworld"了。 但如果以char*p="Helloworld"的方式使用,p指向的是静态字符串存储的位置,也就是说指向"Helloworld"的原件,当然没有问题了。 如果想坚持用charp[]而不使用char*p,有效方法必须是: { staticcharp[]="Helloworld"; returnp; } 原因我想很清楚了。 staticchar[]是静态的,存储在数据区。 提问者评价 多谢大虾的指点! 建立单向动态链表,并对它进行插入、删除和输入等操作,包括以下任务: liapi123|Lv3|被浏览14次|来自360安全卫士 2013-06-146: 52 1、写一个函数creat,用来建立一个动态链表,各结点的数据由键盘输入。 2、写一个函数print,将上题建立的链表中各结点的数据一次输出。 3、写一个函数del,用来删除动态链表中一个指定的结点(由实参指定某一学号,表示要删除该项学生的结点)。 4、写一个函数insert,用来向动态链表插入一个结点。 5、将以上4个函数组成一个程序,有主程序先后调用这些函数,实现链表的建立、输出、删除和插入,在主程序中指定需要删除和插入的结点。 请分别: 1、用一个文件包含这些函数。 2、把每个函数作为一个文件,然后把它们放在一个项目文件中处理 满意回答 #include #defineLENsizeof(structnumber) structnumber/*定义编号和数字*/ intname; intnum; structnumber*next; }; structnumber*create()/*建立链表函数*/ { structnumber*head,*new,*tail,*p; intcount=0; while (1) { new=(structnumber*)malloc(LEN); printf("inputNameandNumber\n"); scanf("%d%d",&new->name,new->num);/*用户输入编号和数字*/ if(new->name==0) { free(new); break; } elseif(count==0) { head=new; tail=new; } else { tail->next=new; tail=new; } count++; } tail->next=NULL; return(head); } structnumber*delist(structnumber*head,intname)/*删除数字的函数*/ { structnumber*p0,*p1; p1=head; if(head==NULL) { printf("\nemptylist! \n"); } else if(p1->name==name)/*找到相同编号*/ head=p1->next; else { while(p1->name! =name&&p1->next! =NULL)/*逐一排查*/ { p0=p1; p1=p1->next; } if(p1->name==name) { p0->next=p1->next; printf("Thenodeisdeleted\n"); } else printf("Thenodecannotbeenfoud! \n"); } returnhead; } structnumber*insert(structnumber*head,structnumber*new) {/*插入函数*/ structnumber*p0,*p1; p1=head; if(head==NULL) { head=new; new->next=NULL; } else if(new->name { new->next=head; head=new; } else { while(new->name>p1->name) { p0=p1; p1=p1->next; } new->next=p0->next; p0->next=new; } return(head); } voidprint(structnumber*head)/*打印函数*/ { structnumber*p; p=head; if(head==NULL)/*其实用不到*/ printf("listisempty\n"); else while(p! =NULL) { printf("%d%d\n",p->name,p->num);/*打印链表内容*/ p=p->next; } } structnumber*find(structnumber*head,structnumber*new) {/*查询函数*/ structnumber*p0,*p1; p1=head; if(head==NULL) { head=new; new->next=NULL; } else if(new->name! =p1->name)/*寻找和输入编号相同的节点,不是则往下读*/ { new->next=head; head=new; } else { while(new->name==p1->name) { printf(”findsuccessfully! \n”); printf(“%d%d”,p1->name,p1->num); } } return(head); } voidmain()/*主函数*/ { structnumber*head,*p; intname; head=create();/*往下逐一调用函数*/ print(head); printf("Inputthedeletenumber: "); scanf("%d",&name); head=delist(head,name); print(head); printf("Inputtheinsertednameandnumber: "); p=(structnumber*)malloc(LEN); scanf("&d&d",p->name,p->num); head=insert(head,p); print(head); printf(“Inputthefoundname: ”); scanf(“%d”,&name); head=find(head,p); print(head); } find函数你可以不用,在主函数中将find函数也一起删掉就行了。 函数实现将一个单向链表的结点倒置排列 2011-05-0615: 58当时就毛了我 | 分类: 数据结构及算法 | 浏览723次 structnode*fun(structnode*head){ Structnode*p,*q; If(head==NULL||________)returnhead; P=head;q=p->next; While(___________){ ______________; q->next=head; head=q;_________;} returnhead; } 那几个空填啥,还有每一句的意思。 分享到: 2011-05-0616: 58提问者采纳 以循环的第一趟为例进行讲解: struct node* fun(struct node*head){ Struct node *p,*q; If(head==NULL||head->next == NULL) //只有链表为空或只有一个结点 return head; P=head; //p从第1个结点起 q=p->next; //q从第2个结点起,并且q几乎总在p的下一个结点位置 while(q ! = NULL) { p->next = q->next; //第1个结点直接指向第3个结点 q->next=head; //q作为新的首结点 head=q; //head重新定位 q = p->next; //p总是出现在p的下一个位置 } return head; } 补充程序: (1)head->next == NULL (2)q ! = NULL (3)p->next = q->next (4)q = p->next 示意图: 用C++实现一链表,其中每个节点都是一个类。 能实现插入、删除、查找等功能 5 满意答案 PK放假 4级 2010-12-27 classLinkNode{ //以一个int举例 public: LinkNode(inti){node=newLinkNode(i); //嗯这是精髓所在} inti; LinkNode*node;//起到牵尾巴的作用 }; template classLinklist private: //Vector LinkNode*head;//纪录头; public: bool add(LinkNode*l);//关键就是LinkNode()构造函数有尾巴,因此可以不停地加结点 booldel(LinkNode&l);//用删除的条件创建Nodel,遍历查找如符合条件则删除并返回true; LinkNode*search(LinkNode*l,LinkNode&index);//输入搜索条件index ,如找到改结点的指针 //anothermethods }; 补充: 刚才的有误 classLinkNode{ //以一个int举例 public: LinkNode(inti){this.i=i} inti; LinkNode*node;//起到牵尾巴的作用 }; template classLinklist private: //Vector LinkNode*head;//纪录头; LinkNode*fence; public: bool add(LinkNode*l);//调用LinkNode构造函数fence->node=newLinkNode(i);这没错了 booldel(LinkNode&l);//用删除的条件创建Nodel,遍历查找如符合条件则删除并返回true; LinkNode*search(T&index);//输入搜索条件index ,如找到改结点的指针否则返回空 //anothermethods }; 编写一个完整的程序,实现单链表的建立、插入、删除、输出等基本操作。 zhanshi122|Lv3|被浏览18次|来自360安全卫士 2013-06-267: 20 1)建立一个带头结点的单链表。 (2)计算单链表的长度,然后输出单链表。 (3)查找值为x的直接前驱结点q。 (4)删除值为x的结点。 (5)把单向链表中元素逆置(不允许申请新的结点空间)。 (6)利用 (1)建立的链表,实现将其分解成两个链表,其中一个全部为奇数,另一个全部为偶数(尽量利用已知的存储空间)。 (7)在主函数中设计一个简单的菜单,分别测试上述算法 满意回答 typedefintElemtype; typedefintstatus; #defineOVERFLOW-2 #defineOK1 #defineERROR-1 #include"stdio.h" #include"stdlib.h" typedefstructLNode{ Elemtypedata; structLNode*next; }*linklist; //构造链表 voidCreate_Linklist(linklist&L) { linklistp; p=(linklist)malloc(sizeof(LNode)); if(! p) exit(OVERFLOW); L=p; L->next=NULL; } //节点插入 voidInsert_Linklist(linklist&L) { linklistp; intn,i; printf("请输入插入节点的个数n: "); scanf("%d",&n); getchar(); for(i=n;i>0;i--) { p=(linklist)malloc(sizeof(LNode)); scanf("%d",&p->data); p->next=L->next; L->next=p; } } //遍历输出并输出长度 statusVisit_linklist(linklist&L) { linklistp; inti=1; p=L->next; if(L->next==NULL) returnERROR; while(p->next! =NULL) { printf("%d",p->data); p=p->next; i++; } printf("%d\n",p->data); printf("长度为: %d\n",i); returnOK; } //查找值为x的直接前驱结点q并输出 voidSearch_linklist(linklist&L) { intx,k=0; linklistp=L,q; printf("输入x: "); scanf("%d",&x); getchar(); if(L->next==NULL) printf("该表为空! \n"); while(p->next! =NULL) { q=p; if(p->next->data==x) { printf("%d",q->data); k=1; } p=p->next; } if(p->next&&p->data==x) { printf("%d",p->data); k=1; } if(k==0) printf("未找到值为%d的结点\n",&x); printf("\n"); } //删除节点 statusDelete_linklist(linklist&L) { linklistp,q; intk=0,x; printf("请输入删除节点的值x: "); scanf("%d",&x); getchar(); if(L->next==NULL) returnERROR; p=L; q=L->next; while(q! =NULL) if(q->data==x) { k=1; p=q; p->next=q->next; free(q); q=p->next; } else { p=q; q=p->next; } if(k==0) printf("表中没有值为%d的结点! \n",&x); returnOK; } //链表逆置 voidLi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 汉克
![提示](https://static.bdocx.com/images/bang_tan.gif)