首页 > 其他分享 >C/指针进阶

C/指针进阶

时间:2024-06-19 16:56:45浏览次数:9  
标签:node head 进阶 struct num student printf 指针

一.指针数组与二维指针

1.指针数组:变量类型 *指针[n],这种形式来定义,比如char *c[3]。如图表示c  c[0]与字符型数据的关系。

#include<stdio.h>
int main(void)
{
	int a[2][3]={1,2,3,4,5,6};
	char *c[3]={"abcd","bbbb","cccc"};

	printf("a=%d\n",a);
	printf("&a[0]=%d\n",&a[0]);
	printf("a[0]=%d\n",a[0]);
	printf("%d  %d  %d\n",&a[0][0],&a[0][1],&a[1][0]);


	printf("c=%d\n",c);
	printf("&c[0]=%d\n",&c[0]);

	printf("*c=%d\n",*c);
	printf("c[0]=%d\n",c[0]);


	printf("c[0]=%s\n",c[0]);
	printf("c=%s\n",c);	
	return 0;
}

输出结果:c是c[0]的地址,c代表的内容*c即c[0]里面存的内容,也是一个指针,指向字符型数据首地址。对c进行字符串输出没有任何意义,因为c这个二维指针并不指向字符串。对于二维数组a[2][3],a=&a[0]=&a[0][0]。

2.不能用二维指针指向二维数组

#include<stdio.h>
int main(void)
{
  char c[2][2]={'a','b','c','d'};
  char *b, **chr;
  chr=c;//错误
  chr=b;//正确
  return 0;
}

不能这样指,因为c与&c[0]以及&c[0][0]一样,如果chr=c,*chr='a',但实际上*chr指向的还是地址,关键就是二维数组的 &c[0]和&c[0][0]地址一样,相当于二维指针中间少了一维。

二,链表学习

//利用链表构建
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student_node
{
	int num;
	char name[20];               
	int score;
	struct student_node *next;
};
struct student_node *build(struct student_node *head);//新建链表
struct student_node *delete1(struct student_node *head,int num);//删除链表
void print(struct student_node *head);//遍历链表
int main(void)
{
	int chioce;
	int num;
//	char name[20];
	int size=sizeof(struct student_node);
	struct student_node *head;
	do{
		printf("1:build  2:delete1  3:print  0:exit");
		scanf("%d",&chioce);
	    switch(chioce)
		{
		case 1:
			head=build(head);
			break;
	
		case 2:
			printf("delete num:");
			scanf("%d",&num);
			head=delete1(head,num);
			break;
		case 3:
			print(head);
			break;
		case 0:
			break;
		}
	}while(chioce!=0);
	return 0;
}
struct student_node *build(struct student_node *head)
{
	struct student_node *p,*end;
	int num,score;
	char name[20];
	head=NULL;
	end=head;
	printf("put in:num  name score ");
    scanf("%d%s%d",&num,name,&score);
	int size=sizeof(struct student_node);
	while(num!=0)
	{
		p=(student_node*)malloc(size); //开辟动态存储空间   
		p->num=num;
		strcpy(p->name,name);
		p->score=score;
		p->next=NULL;

		if(head==NULL)//为空,直接添加
		{
		head=p;
		head->next=NULL;
		end=head;
		scanf("%d%s%d",&num,name,&score);
		}
		else {
		end->next=p;
		end=p;
		scanf("%d%s%d",&num,name,&score);
		}
	}
	return head;
}
struct student_node *delete1(struct student_node *head,int num)
{
	struct student_node *ptr1,*ptr2;
	if((head!=NULL)&&(head->num==num))
	{
		ptr1=head;
		head=head->next;
		free(ptr1);
	}
	if(head==NULL)
	{
		return NULL;
	}
	ptr1=head;
	ptr2=head->next;
	while(ptr2!=NULL)
	{
		if(ptr2->num==num)
		{
			ptr1->next=ptr2->next;
			free(ptr2);
		}
		else 
		{
			ptr1=ptr2;
			ptr2=ptr1->next;
		}
	}
	return head;

}
void print(struct student_node *head)
{
	struct student_node *ptr;
	ptr=head;
	if(ptr==NULL)
	{
		printf("nothing");
		return;
	}
	printf("All data:\n");
	printf("num\tname\tscore\n");
	for(ptr=head;ptr!=NULL;ptr=ptr->next)
	{
		printf("%d\t%s\t%d\n",ptr->num,ptr->name,ptr->score);
	}
}

三,杂记

1.为什么定义函数里面形参定义的是数组,但是当带入实参的时候带入的是数组的地址,节省内存空间,假如数组非常大的话,拷贝过去肯定会加大内存开销。

2.函数嵌套,用指针指向该被嵌套的函数。

//对于函数套函数
#include<stdio.h>
#include<math.h>
double f1(double x);
double calc(double(*f)(double),double a,double b);
int main(void)
{
	double m;
	double a,b;
	printf("输入a和b:");
	scanf("%lf%lf",&a,&b);
	m=calc(f1,a,b);
	printf("结果:%lf",m);
	return  0;
}
double f1(double x)
{
	return x*x;
}
double calc(double(*f)(double),double a,double b)// 指针指向的函数怎么表示:函数返回类型(*指针)(函数使用数据类型)
{
	double result;
	result=(b-a)/2*((*f)(a)+(*f)(b));
	return result;
}

标签:node,head,进阶,struct,num,student,printf,指针
From: https://blog.csdn.net/xxxx123445/article/details/139806261

相关文章

  • EarMaster pro 7 For Mac软件下载-EarMaster Pro(音乐赏析进阶专业版)V6.1下载附加详
    EarMasterpro7ForMac最新版是一款相当专业的听力训练和音乐理论练习软件,这个EarMasterpro7ForMac版带来了全的操作界面,并且更新了课程信息,内置了2000多种课程供用户选择,可以用于听耳训练、视唱和节奏训练等操作。安装包获取地址:EarMasterProwin版:​​https......
  • 代码随想录算法训练营第四十三天 | 完全背包理论基础、518.零钱兑换II、377. 组合总和
    完全背包理论基础题目链接:https://kamacoder.com/problempage.php?pid=1052文档讲解:https://programmercarl.com/%E8%83%8C%E5%8C%85%E9%97%AE%E9%A2%98%E7%90%86%E8%AE%BA%E5%9F…视频讲解:https://www.bilibili.com/video/BV1uK411o7c9/思路完全背包中,每个物品可以......
  • this指针小总结
    目录this指针小总结this指针在类中和全局中的区别this指针和普通指针的区别this指针的用法this指针小总结在C++中,this指针是一个隐式的、非静态的成员指针,它指向调用它的对象的地址。每个非静态成员函数都含有一个this指针,该指针在成员函数中用于访问调用它的对象的成......
  • 排序算法进阶
    1.归并排序简介归并排序基于分治思想将数组分段排序后合并,时间复杂度在最优、最坏与平均情况下均为O(nlogn) ,空间复杂度为O(n)。归并排序可以只使用O(1) 的辅助空间,但为便捷通常使用与原数组等长的辅助数组。归并排序最核心的部分是合并(merge)过程:将两个有序的数组......
  • 【笔记】指针函数中可以返回什么样的指针,从两个例子开始(上)
    关于指针函数的两个例子:第一个例子:下面这段代码是否能正确运行?#include<stdio.h>char*get_string(){char*s="hello";returns;}intmain(){char*p;p=get_string();printf("p=%s\n",p);}因为s指向的是字符串常量,被存储在静态区(......
  • 【C语言】数组参数和指针参数详解
    在写代码的时候难免要把【数组】或者【指针】传给函数,那函数的参数该如何设计呢?1一维数组传参#include<stdio.h>voidtest(intarr[])//ok?{}voidtest(intarr[10])//ok?{}voidtest(int*arr)//ok?{}voidtest2(int*arr[20])//ok?{}voidtest2(int**arr)//ok?......
  • 【暑假Python上岸计划】最新20+Python实战案例,全程干货,30天看完即可接单就业!(基础+进阶
    前言今天给大家分享20+个基于python的实战案例,主要包含:数据分析、可视化、机器学习/深度学习、时序预测等,案例的主要特点:*提供源码:代码都是基于jupyternotebook,附带一定的注释,运行即可*数据齐全:大部分案例都有提供数据,部分案例使用内置数据集学习资料已打包,需要......
  • 深度神经网络进阶
    深度神经网络进阶深度神经网络(DeepNeuralNetworks,DNNs)是现代人工智能和机器学习的重要工具。它们在图像识别、自然语言处理、游戏智能等领域取得了显著的成果。本文将探讨一些深度神经网络的进阶主题,帮助您更深入地理解和应用这些强大的模型。目录深度神经网络的架构......
  • 当char型变量遇上char*型的指针
    #include<stdio.h>intmain(void){ char*i=(char*)0x1111; printf("size=%d%d\n",sizeof(i),sizeof((char*)0x11));//sizei=8bytes,size(char*)0x11=8bytes,cause0x11isconveredtochar*,char*isaponinterandhas64......
  • 吴恩达机器学习 第二课 week3 学习算法(模型)进阶
    目录01学习目标02导入计算所需模块03多项式回归模型进阶3.1数据集划分3.2 寻找最优解3.3 正则优化3.4增大数据量04神经网络模型进阶4.1数据准备4.2模型复杂度4.3正则优化05总结01学习目标   (1)掌握多项式回归模型的求解和优化   (2)掌握神......