实训第三天日志

学习日志                                                   姓名:成盛       班级:通卓161

今日学习任务 指针
指针和数组
指针和变量
指针和字符串
今日任务完成情况 基本完成
今日开发中出现的问题汇总 对指针的含义理解不够深刻
今日未解决问题 用指针代替数组进行字符串变位
今日开发收获

1.++*p先对*p+1;  *p++;先*p然后p的地址加所对应的数据类型的字节。malloc:申请合法内存 返回值void *   头文件#include<stdlib.h> #include<malloc.h>   char *ptr = (char *)malloc(sizeof(char) * 64)

2. int *p1 = (int *)(&a+1); //设a地址0x100     &a代表数组 &a+1跳过整个数组   0x114

int *p2 = (int *)((int)a + 1);          //原来a是地址被强制转换为整型 变为+1 p2指向a[0]的第二个字节0x101  

3. char str[20];   //在栈上面分配20个字节的内存  str有自己的地址    str = "helloworld";     //不合法 , 常量在代码段(有固定的地址)  //str是常量不能修改 ptr是变量

4. char *ptr; //野指针  prt = "helloworld";   //指针的赋值    ptr[0] = 'a',不合法 helloworld是常量不能修改

自我评价 一般 其他 无

#include<stdio.h>

#include<stdlib.h>




int sort(int a,char *p[])
{
int i;
char *get[100] = {0}  ;
for(i = 0; i < a ;i++)
{
get[i] = (char *)malloc(sizeof(char) * 32);        
if(NULL == get[i])
{
printf("malloc failure!\n");
return -1;
}
        get[i] = p[a -1 -i];
printf("%s ",get[i]);  
}
printf("\n");
}


int main()
{
int i,num;
char *string[100] = {0};

printf("输入字符串的个数:\n");
scanf("%d",&num);
printf("输入字符串:\n");
for(i = 0;i < num; i++)
{
string[i] = (char *)malloc(sizeof(char) * 32);        //每一段存放在堆空间  32字节 返回每个空间的首地址
if(NULL == string[i])
{
printf("malloc failure!\n");
return -1;
}
scanf("%s",string[i]);                      //把字符串放入指定空间
}
    sort(num,string);

}

猜你喜欢

转载自blog.csdn.net/cheng1064233793/article/details/81004737