linux c 指针

判断指针定义或是否正确:

1 看语法 =右边是否为地址

2 看两边字节大小是否相等

函数指针: 是一种指针, 指针指向 函数所在的地址 

int (*p)()  int表示 函数的返回值是int类型(整形),()形参类型。

要理解 下列代码的意思

函数指针

#include <stdio.h>

void print()
{
	printf("helloworld\n");
}

int add(int x, int y)
{
	return (x + y);
}

int main()
{
	void (*p)();  //函数指针 指向一个没有形参、没有返回值的函数
	p = print;
	//print();
	p();

	int (*q)(int, int);
	q = add;
	q(1, 2);

	return 0;
}

 指针函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *Init()   //指针函数
{
	char *tmp = (char *)malloc(sizeof(char) * 10);

	return tmp;
}

int main()
{
	char *str;

	str = Init();

	strcpy(str, "hello");
	return 0;
}

 数组指针

#include <stdio.h>

int main()
{
	int i, j;
	int a[3][4] = {1, 2, 3, 4 ,5, 6, 7, 8, 9, 10, 11, 12};
	int (*p)[4] = a;//数组指针
	//int **q = a;

	for (i = 0; i < 3; i++)
	{
		for (j = 0; j < 4; j++)
		{
			//printf("%d ", a[i][j]);
			//printf("%d ", *(*(p + i) + j));
			//printf("%d ", p[i][j]);
			printf("%d ", (*(p + i))[j]);
		}
		printf("\n");
	}

	return 0;
}

 这个重点理解

#include <stdio.h>

int main(int argc, char *argv[])
{
	//argc参数的个数 包括./5
	//argv 具体的参数 指针数组

	int i;

	for (i = 1; i < argc; i++)
	{
		printf("%s\n", argv[i]);
	}
	return 0;
}

 

 这个重点理解

#include <stdio.h>

int main()
{
    char *name[] = {"hello","world","i am","xinge"};
    char **p;
    int i;
    for(i = 0;i < 4;i++)
    {
        p = name + i;
        printf("%s\n",*p);
    }
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char live(char **str)
{
   *str = (char*)malloc(sizeof(char) * 100);
}

main()
{
    char *ptr = NULL;
    live(&ptr);
    strcpy(ptr,"hello world!");    
    printf("%s\n",ptr);

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值