int get_length(char str[]) { char *p = str; int count = 0; while (*p++ != '\0') { count++; } return count; }这一串代码是什么意思
时间: 2024-02-14 22:14:12 浏览: 126
这段代码是一个函数,函数名为 `get_length`,它接收一个字符数组 `str` 作为参数,并返回数组中字符的个数。
函数中定义了一个指向字符数组的指针 `p`,并初始化为指向 `str` 数组的第一个元素。
通过 `while` 循环遍历整个数组,当指针 `p` 指向的字符不是字符串结束符 '\0' 时,计数器 `count` 就加1,直到遍历结束。
最后,函数返回计数器 `count` 的值,即字符串中字符的个数。
相关问题
#define MAX_LINE_LENGTH 1024 #define MAX_RECORDS 5 char command[] = "/usr/bin/sn_core.elf getstat 2>/dev/null"; char line[MAX_LINE_LENGTH]; const char *filename = "data.txt"; float temp; char *Temp[MAX_RECORDS] = {0}; int index = 0; void parse_temperature(char *line) { char *temp_str; char *token; // 解析温度 if ((temp_str = strstr(line, "temp: ")) != NULL) { temp_str += strlen("temp: "); temp = strtof(temp_str, NULL); } } void add_record(float temp) { if (index >= MAX_RECORDS) { free(Temp[0]); memmove(Temp, Temp + 1, (MAX_RECORDS - 1) * sizeof(char *)); index--; } Temp[index] = malloc(sizeof(float)); memcpy(Temp[index], &temp, sizeof(float)); index++; } void print_records() { for (int i = 0; i < index; i++) { printf("%f\n", *((float *)Temp[i])); } } int main() { FILE *fp,*fd; while (1) { fp = popen(command, "r"); if(fp == NULL){ printf("Error running command.\n"); exit(1); } fd = fopen(filename,"w"); if(fd == NULL){ printf("Error open file!\n"); exit(1); } while (fgets(line, MAX_LINE_LENGTH, fp) != NULL) { //printf("%s\n",line); parse_temperature(line); } pclose(fp); add_record(temp); for(int i=0;i<MAX_RECORDS;i++){ fprintf(fd,"%s\n",Temp[i]); } //print_records(); sleep(1); } return 0; } 优化代码
下面是经过优化的代码,主要是对部分变量进行了重命名,并且使用了更安全的动态内存分配函数:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define MAX_LINE_LENGTH 1024
#define MAX_RECORDS 5
const char *command = "/usr/bin/sn_core.elf getstat 2>/dev/null";
const char *filename = "data.txt";
float temperature;
char *temperature_records[MAX_RECORDS] = {NULL};
int record_count = 0;
void parse_temperature(char *line) {
char *temp_str;
char *token;
// 解析温度
if ((temp_str = strstr(line, "temp: ")) != NULL) {
temp_str += strlen("temp: ");
temperature = strtof(temp_str, NULL);
}
}
void add_temperature_record(float temperature) {
if (record_count >= MAX_RECORDS) {
free(temperature_records[0]);
memmove(temperature_records, temperature_records + 1, (MAX_RECORDS - 1) * sizeof(char *));
record_count--;
}
temperature_records[record_count] = (char *)malloc(sizeof(float));
memcpy(temperature_records[record_count], &temperature, sizeof(float));
record_count++;
}
void print_temperature_records() {
for (int i = 0; i < record_count; i++) {
printf("%f\n", *((float *)temperature_records[i]));
}
}
int main() {
FILE *pipe, *file;
while (1) {
pipe = popen(command, "r");
if (pipe == NULL) {
printf("Error running command.\n");
exit(1);
}
file = fopen(filename, "w");
if (file == NULL) {
printf("Error open file!\n");
exit(1);
}
char line[MAX_LINE_LENGTH];
while (fgets(line, MAX_LINE_LENGTH, pipe) != NULL) {
parse_temperature(line);
}
pclose(pipe);
add_temperature_record(temperature);
for (int i = 0; i < record_count; i++) {
fprintf(file, "%s\n", temperature_records[i]);
}
sleep(1);
}
return 0;
}
```
char字符数组长度
### 获取C/C++中 `char` 字符数组的长度
在 C/C++ 中,可以通过多种方式来确定字符数组的实际长度。以下是几种常见的方式:
#### 使用 `strlen()` 函数
对于字符数组而言,标准库提供了 `strlen()` 函数专门用来计算不带终止符 `\0` 的字符串长度[^3]。
```c
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
printf("Length of string using strlen(): %lu\n", strlen(str));
return 0;
}
```
此方法适用于以 null 结尾 (`\0`) 的字符串数据;如果不是这种情况,则需要采用其他手段。
#### 利用手动遍历计数
当面对非null结尾的数据或者想要自定义逻辑时,可以手动编写循环来进行统计:
```c
#include <stdio.h>
int get_char_array_length(const char *arr) {
int count = 0;
while (arr[count] != '\0') ++count;
return count;
}
int main(){
const char myArray[]="example text.";
printf("Manual counting result:%d\n",get_char_array_length(myArray));
return 0;
}
```
这种方法虽然简单直观,但在实际项目里通常推荐使用更高效稳定的内置函数如 `strlen()` 来代替手工实现。
#### 对于固定大小的字符数组利用 `sizeof` 操作符
如果已知整个数组的空间分配情况,也可以借助编译期常量表达式——即 `sizeof` 操作符配合除法运算得出元素数量[^4]:
```cpp
const unsigned int array_size = sizeof(char_array)/sizeof(char);
// 或者针对多维或多元素类型的情况调整分母部分
```
需要注意的是,这种方式仅限于静态声明且未被指针化的情况下有效。一旦传递给另一个作用域内的子程序调用,原始尺寸信息将会丢失,因此建议显式地将所需维度一同传送进去。
阅读全文
相关推荐
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)