不理解指针的问题

C语言 码拜 8年前 (2016-06-09) 723次浏览
在主函数把45 行换成 46或47 这样的代码就会报错,不清楚是为什么,请高手解答。谢谢。

/* 查询字符串中最长数字的长度及数字 */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//查询字符串中最长数字的长度及数字
void findNum(char *str, char **ppos, int *length)
{
	char *pstr = str;
	char *ptemp;
	int sum =0;
	while (*pstr != "\0")
	{	//假如不是数字就跳过
		while ((*pstr <"0" || *pstr>"9") && *pstr != "\0")
		{ 
			pstr++;
		}
		//记录查询到的数字
		if (*pstr >= "0" && *pstr <= "9")
		{
			//pos:开始处
			ptemp = pstr;
			int temp = 0;
			while (*pstr >= "0" && *pstr <= "9")
			{
				pstr++;
				temp++;
			}
			if (temp > sum)
			{
				sum = temp;
				//printf("*ppos:%p\n", *ppos);
				*ppos = memcpy(*ppos, ptemp, sum); //把查询到的内容拷贝到*ppos中
				//printf("*ppos:%p\n", *ppos);
			}
		}
	}
	*length = sum; //记录最长字符的数值
}
void main()
{
	char str[] = "abc12356abc09876543210abc789q";
	char des[50] = "0"; //des:是常量不能修改指向。
	//num:保存字查询到的最长字符串
	char *num = des;  // 假如换成  char *num = "0"; 或 char *num = "0"; 都不可以呢?
	//char *num = "0";  1:处
	//char *num ;		2:处
	int length ;	//记录数字的长度
	printf("  num:%p\n", num);
	findNum(str, &num, &length);
	printf("%s,%d\n", num, length);

	/*char *p = "0";
	char a[10] = "123";
	p = a;
	printf("%s\n", p);*/
	system("pause");
}
解决方案

40

char *num=”0″  num指向的内容是常量,不能改
式子等同于 const char *p=”0″;

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明不理解指针的问题
喜欢 (0)
[1034331897@qq.com]
分享 (0)