Sizeof() and Strlen()的区别-20151026

  • Sizeof()是一个运算符,而不是一个函数。
  • 对于参数不同时,sizeof返回值表示的含义也是不同的:

**数组—分配的数组空间大小
指针—存储指针所用的空间大小
类型—该类型所占的空间大小
对象—对象实际占用空间的大小
函数—函数返回类型所占的空间大小**

  • Strlen()是函数,需要头文件”string.h”
  • 参数必须是字符型指针(char*),且必须是以’\0’结尾的。 当数组名作为参数传入时,实际上数组就退化成了指针
Eg:
int eg[10];
cout<< sizeof( eg ) <<endl; 
cout<< strlen( eg ) <<endl;

[Error] cannot convert 'int*' to 'const char*' for argument '1' to 'size_t strlen(const char*)'

改变 eg[10]的类型:

char eg[10];
cout<< sizeof( eg ) <<endl; 
cout<< strlen( eg ) <<endl;
10
0
--------------------------------
Process exited after 0.3467 seconds with return value 0

Sizeof返回定义arr数组时,编译器为其分配的数组空间大小,不关心里面存了多少数据。
Strlen只关心存储的数据内容,不关心控件的大小和类型

    char x[10];
    char y[10];
    char z[10] = "";
    cout<<" The result is : "<<endl;
    cout<<"sizeof(x) = "<<sizeof( x )<<"\tstrlen(x) = "<<strlen( x )<<endl;
    cout<<"sizeof(y) = "<<sizeof( y )<<"\tstrlen(y) = "<<strlen( y )<<endl; 
    cout<<"sizeof(z) = "<<sizeof( z )<<"\tstrlen(z) = "<<strlen( z )<<endl; 

The result is :
sizeof(x) = 10  strlen(x) = 3
sizeof(y) = 10  strlen(y) = 1
sizeof(z) = 10  strlen(z) = 0

--------------------------------
Process exited after 0.2898 seconds with return value 0
请按任意键继续. . .

实际上每次运行可能不一样,这取决于指针里面存了什么(从parr[0]开始知道遇到第一个NULL结束);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是对代码的一些优化建议: 1. 变量类型声明放在代码块的开始处,方便查看代码结构和变量类型。 2. 将相同类型的变量放在一起,提高代码可读性。 3. 将常量定义为宏,增加代码的可维护性和可读性。 4. 对于频繁使用的字符串常量,可以定义为全局变量,避免重复的字符串拷贝操作。 5. 优化数组操作,可以将数组操作封装成函数,提高代码复用性。 6. 对于数组的插入和删除操作,可以使用循环队列来实现,提高效率。 下面是优化后的代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #define MAX_LINE_LENGTH 1024 #define MAX_RECORDS 50 char command[] = "/usr/bin/sn_core.elf getstat"; char line[MAX_LINE_LENGTH]; char time[MAX_LINE_LENGTH]; float temp; int time_ns; char *array1[MAX_RECORDS] = {0}; char *array2[MAX_RECORDS] = {0}; int index = 0; void parse_temperature_and_time(char *line) { char *temp_str, *time_str; char *token; // 解析温度 if ((temp_str = strstr(line, "temp: ")) != NULL) { temp_str += strlen("temp: "); temp = strtof(temp_str, NULL); } // 解析时间 if ((time_str = strstr(line, "Time: ")) != NULL) { time_str += strlen("Time: "); strncpy(time, time_str, MAX_LINE_LENGTH); time[strlen(time) - 1] = '\0'; token = strtok(time, " +"); while (token != NULL) { if (strchr(token, 'n') != NULL) { time_ns = strtol(token, NULL, 10); break; } token = strtok(NULL, " +"); } } } void add_record(float temp, char *time) { if (index >= MAX_RECORDS) { free(array1[0]); free(array2[0]); memmove(array1, array1 + 1, (MAX_RECORDS - 1) * sizeof(char *)); memmove(array2, array2 + 1, (MAX_RECORDS - 1) * sizeof(char *)); index--; } array1[index] = malloc(sizeof(float)); memcpy(array1[index], &temp, sizeof(float)); array2[index] = malloc(sizeof(char) * MAX_LINE_LENGTH); strcpy(array2[index], time); index++; } void print_records() { for (int i = 0; i < index; i++) { printf("温度:%f\n", *((float *)array1[i])); printf("时间: %s\n", array2[i]); } } int main() { FILE *fp; while (1) { fp = popen(command, "r"); while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) { parse_temperature_and_time(line); } pclose(fp); add_record(temp, time); print_records(); sleep(1); } return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值