strlen()和sizeof的区别&数组和指针

从入口参数接过来的数据最好定义个临时变量,做个缓冲,不要轻易修改原地址的值,要引进一个辅助的指针变量把形参接过来,很重要。一定要清楚指针在内存中的指向,不然会指向一个错误的地址。

void get(char *from, char *to)
{
char *myfrom = from;
char *myto = to;
}

strlen是函数,字符串长度,不包括停止符。而sizeof则是内存块的大小,包括停止符。
数组是一种数据类型,数据类型的本质就是固定大小,内存块的别名。可以用sizeof()一般都是数据类型。

int main()
{
char buf[] = "abcd";
printf("%d\n",strlen(buf));
printf("%d\n",sizeof(buf));
}

输出结果就是4,5

看下面的这个程序是错误的,buf是分配在全局区的,是个常量,是只读的,不允许被修改,buf是字符a的首地址。在c++中一般应定义为const类型。

int main()
{
char *buf = "abcd";
buf = buf+1;//error
}

但是你可以这么写:一定要记住buf是个常量,它表示数组首地址。

int main()
{
char *buf = "abcd";
char *p = buf;
p = p+1;//error
}

/************************************
字符串拷贝copy[i] = '\0';丢了,打印的时候不会停止,因为printf是遇到终止符才停止打印。下面是几个经典的字符串拷贝函数。
************************************/如果copy被定义为*copy,程序是无法运行的,因为copy无法分配内存的,程序会卡掉。

int main()
{
    char buf[] = "I am a c studer!";
    char *copy ;
     printf("%p\n",copy);
    int i;
    for(i = 0; *(buf + i) != '\0';i++)
    {
        *(copy+i) = *(buf+i);
    }
    copy[i] = '\0';
    printf("%s\n",buf);
    printf("%s\n",copy);
}
//(nil)
//Segmentation fault (core dumped)
int main()
{
    char buf[] = "I am a c studer!";
    char copy[64] ;
    int i;
    for(i = 0; *(buf + i) != '\0';i++)
    {
        *(copy+i) = *(buf+i);
    }
    copy[i] = '\0';
    printf("%s\n",buf);
    printf("%s\n",copy);
}
#define  _CRT_SECURE_NO_WARNINGS 
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

void main71()
{
	char a[] = "i am a student";
	char b[64];
	int  i = 0;

	for (i=0; *(a+i) != '\0'; i++)
	{
		*(b+i) = *(a+i);
	}

	b[i] = '\0'; //ÖØÒª
	printf("a:%s \n", a);
	printf("b:%s \n", b);

	system("pause");
	return ;
}


void copy_str21(char *from, char *to)
{
	for (; *from!='\0'; from++, to++)
	{
		 *to = *from;
	}
	*to = '\0';

	return ;
}


void copy_str22(char *from, char *to)
{
	for (; *from!='\0';)
	{
		*to++ = *from++;  //  ÏÈ *to = *from;  ÔÙfrom++, to++ 
	}
	*to = '\0'; //

	return ;
}

void copy_str23(char *from, char *to)
{
	while( (*to = *from) != '\0' )
	{
		from ++; 
		to ++;
	}
}

void copy_str24(char *from , char *to)
{
	while ( (*to++ = *from++) != '\0')
	{
		;
	}
}


void copy_str25(char *from , char *to)
{
	//*(0) = 'a';
	while ( (*to++ = *from++) )
	{
		;
	}
}


void copy_str25_err(char *from , char *to)
{
	//*(0) = 'a';
	while ( (*to++ = *from++) )
	{
		;
	}

	printf("from:%s \n", from);
	
}



int copy_str26_good(char *from , char *to)
{
	//*(0) = 'a';
	char *tmpfrom = from;
	char *tmpto = to;
	if ( from == NULL || to == NULL)
	{
		return -1;
	}


	while ( *tmpto++ = *tmpfrom++ ) ;  //¿ÕÓïŸä

	printf("from:%s \n", from);
		
}

int main111()
{
	int ret = 0;
	char *from = "abcd";
	char buf2[100]; 
	//copy_str21(from, buf2);
	//copy_str22(from,buf2);
	//copy_str23(from, buf2);
	//copy_str24(from, buf2);
	//copy_str25(from ,buf2);
	//printf("buf2:%s \n", buf2);
	system("pause");
	return ret;
}

 

字符串aaaa首先会在全局区分配内存,然后再进行一个到栈区的拷贝工作。

### strlensizeof在计算指针数组大小时的区别 在C/C++中,`strlen` `sizeof` 是两个完全不同的工具,用于不同的场景。以下是它们在计算指针数组大小时的具体区别: #### 1. **`sizeof` 的作用** `sizeof` 是一个编译期运算符,用于计算其操作数的内存大小(以字节为单位)。当应用于指针数组时,`sizeof` 返回的是指针数组本身的大小,而不是它指向的内容的大小。例如,如果指针数组是一个包含多个指针数组,`sizeof` 计算的是这些指针所占的总内存大小,而不是每个指针指向的字符串或数据结构的大小。 对于64位系统,指针通常占用8字节,因此指针数组的大小可以通过以下公式计算: ```cpp size_of_pointer_array = number_of_pointers * size_of_each_pointer ``` 示例代码如下: ```cpp #include &lt;iostream&gt; using namespace std; int main() { const char* arr[] = {&quot;hello&quot;, &quot;world&quot;, &quot;example&quot;}; cout &lt;&lt; &quot;Size of pointer array: &quot; &lt;&lt; sizeof(arr) &lt;&lt; &quot; bytes&quot; &lt;&lt; endl; // 输出 24 bytes (3 pointers * 8 bytes each) return 0; } ``` 在这个例子中,`sizeof(arr)` 返回的是指针数组本身占用的内存大小,即 24 字节(3 个指针 &times; 8 字节)[^1]。 #### 2. **`strlen` 的作用** `strlen` 是一个运行时函数,用于计算以 `\0` 结尾的 C 风格字符串的长度。它不适用于直接计算指针数组的大小。如果尝试将 `strlen` 应用于指针数组,则会导致未定义行为,因为 `strlen` 期望的是一个指向字符数组指针,而不是一个指针数组。 示例代码如下: ```cpp #include &lt;iostream&gt; #include &lt;cstring&gt; using namespace std; int main() { const char* arr[] = {&quot;hello&quot;, &quot;world&quot;, &quot;example&quot;}; // 错误用法:试图使用 strlen 计算指针数组的大小 // cout &lt;&lt; &quot;Length of pointer array: &quot; &lt;&lt; strlen(arr) &lt;&lt; endl; // 编译错误或运行时崩溃 return 0; } ``` 在上述代码中,`strlen(arr)` 是非法的,因为 `arr` 是一个指针数组,而 `strlen` 只能处理单个以 `\0` 结尾的字符串。 #### 3. **总结两者的区别** - **`sizeof`**: - 计算的是指针数组本身的大小。 - 是一个编译期常量。 - 不依赖于指针指向的内容。 - 对于指针数组,返回值是数组指针的数量乘以每个指针的大小。 - **`strlen`**: - 计算的是以 `\0` 结尾的字符串的长度。 - 是一个运行时函数。 - 仅适用于单个字符串,不能直接用于指针数组。 - 返回值不包括终止符 `\0` 的位置。 #### 4. **特殊情况:指针数组中的字符串长度** 如果需要计算指针数组中每个字符串的长度,可以对每个指针调用 `strlen` 函数。例如: ```cpp #include &lt;iostream&gt; #include &lt;cstring&gt; using namespace std; int main() { const char* arr[] = {&quot;hello&quot;, &quot;world&quot;, &quot;example&quot;}; for (int i = 0; i &lt; 3; ++i) { cout &lt;&lt; &quot;Length of string &quot; &lt;&lt; i &lt;&lt; &quot;: &quot; &lt;&lt; strlen(arr[i]) &lt;&lt; endl; } return 0; } ``` 输出结果将是: ``` Length of string 0: 5 Length of string 1: 5 Length of string 2: 7 ``` 这里,`strlen` 被用来计算每个指针指向的字符串的实际长度[^2]。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值