1.字符串连接
这里是传递字符串指针的问题,开始是把*p写在方法里面的,然后传会p指针,但是因为他是被malloc出来的,所以必须free()掉,看了一下论坛上:“ 有个原则应该尽量遵循,那就是分配和回收内存应该尽可能地在同一个作用域内完成。最好避免在子程序中分配空间,在父程序中回收的做法。要绝对避免在父程序中分配空间,放在子程序中回收地做法,除非改子程序地唯一功能就是回收内存”。
还有就是char a[] = "JKLMN",是一个含有六个函数的数组,因为还包括'/0';
#include "stdafx.h"
#define LEN 15
#define LONGER a>b?a:b
void strAdd(char *p,char srtFront[],char srtBack[])
{
int a,b,c,i;
a =(int)strlen(srtFront);
b =(int)strlen(srtBack);
c = a+b+1;
i = LONGER;
for(int mars =0;mars {
if(mars<=a)
*(p+mars) = srtFront[mars];
if(mars<=b)
*(p+mars+a)= srtBack[mars];
}
*(p+c) = '/0';
}
int _tmain(int argc, _TCHAR* argv[])
{
char a[] = "ABCDEFGHI";
char b[] = "JKLMN";
char *p;
p = (char*)malloc(sizeof(char)*(15));
strAdd(p,a,b);
printf("%s/n",p);
free(p);
system("pause");
return 0;
}
2.单链表添加、删除节点。
主要就是理解链表中处理节点的方式就好了
#include
#include
#include
struct LNode
{
int number;
struct LNode *next;
}*Linklist,*p1,*p2,*New,*Pri;
void CreatList(struct LNode *L,int n)
{
int i;
for(i= 0;i
struct LNode *p;
p = (struct LNode *)malloc(sizeof(struct LNode));
printf("/n please enter the int:");
scanf("%d",&p->number);
if(i==0)
{
(*L).number = (*p).number;
free(p);
continue;
}
p->next = L->next;
L->next = p;
printf("/n the number struct is Created/n");
}
}
void ListInsert()
{
int ix,insertSite;
printf("please enter the insertSite:");
scanf("%d",&insertSite);
//struct LNode *p1;
p1 = (struct LNode *)malloc(sizeof(struct LNode));
p1 = Linklist;
ix = 1;
while(Linklist && ix
Linklist = Linklist->next;
ix++;
}
//struct LNode *New;
New = (struct LNode *)malloc(sizeof(struct LNode));
printf("please enter the int :");
scanf("%d",&New->number);
if(insertSite ==1)
{
New->next = Linklist;
Linklist = New;
return;
}
New->next = Linklist->next;
Linklist->next = New;
printf("the number has inserted/n");
Linklist = p1;
free(p1);
}
void ListDeleted()
{
int site,i;
printf("please enter the int:");
scanf("%d",&site);
p1 = (struct LNode *)malloc(sizeof(struct LNode));
p1 = Linklist;
i=1;
while(i
Linklist = Linklist->next;
i++;
}
//struct LNode *p2;
p2 = (struct LNode *)malloc(sizeof(struct LNode));
p2 = Linklist->next;
if(site ==1)
{
Linklist = p2;
free(p1);
return;
}
else
{
Linklist->next = p2->next;
Linklist = p1;
free(p2);
}
}
int Controller()
{
int i;
printf("The 1 is Create/n");
printf("The 2 is Inserte/n");
printf("The 3 is Delete/n");
printf("The 4 is Print/n");
printf("The 0 is Exit/n");
printf("please enter the oparetor: ");
scanf("%d", &i);
switch(i)
{
case 1:
Linklist = (struct LNode *)malloc(sizeof(struct LNode));
Linklist->next = NULL;
CreatList(Linklist,3);
return 1;
case 2:
ListInsert();
return 1;
case 3:
ListDeleted();
return 1;
case 4:
//struct LNode *Pri;
Pri = (struct LNode *)malloc(sizeof(struct LNode));
Pri = Linklist;
while(Pri)
{
printf("%d/n",(*Pri).number);
Pri = Pri->next;
}
return 1;
case 0:
return 0;
}
}
int main(int argc, char *argv[])
{
int i = Controller();
while(i=1)
{
Controller();
}
system("PAUSE");
return 0;
}
3.测试大小端法
int i = 1;
char *pc = (char*)(&i);
pc++;
if(*pc==0)
printf("小端/n");
else
printf("大端/n");
int型1的十六进制表示是0x00000001,在小端法机器中存储成01000000,当pc指向下一个地址时不等于1,也就是等于0,说明机器采用的是小端法,否则是大端法