sprintf—— char* str2 = "ABCDE"; str2[2] = 'C';//错误
时间: 2024-04-14 18:26:13 浏览: 25
在这段代码中,你将一个字符串常量赋值给了一个`char*`指针变量`str2`。然后你尝试修改了`str2`指向的字符数组中的某个字符,这是错误的。字符串常量是不可修改的,因此在尝试修改它时会导致未定义的行为。
要解决这个问题,你可以将`str2`声明为一个字符数组,并使用`strcpy`函数将字符串常量复制到该数组中。这样,你就可以修改数组中的元素了。下面是一个示例:
```c
char str2[] = "ABCDE";
str2[2] = 'C'; // 现在这个操作是有效的
```
请注意,这里的`str2`是一个字符数组,其大小由初始化的字符串常量决定,并且可以修改其元素。
相关问题
c语言 检查一下下面的代码 为什么函数中获取不到键值#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/socket.h> #include <arpa/inet.h> #include <openssl/ssl.h> #include <openssl/err.h> #include <openssl/hmac.h> #include <jansson.h> #include <time.h> #include <errno.h> #include <resolv.h> #include <netdb.h> char* calculate_signature(char* json_str, char* key) { json_t *root; json_error_t error; /* 从文件中读取 JSON 数据 */ root = json_load_file(json_str, 0, &error); /* 遍历 JSON 对象中的所有键值对,并获取键的名称 */ int key_count = json_object_size(root); printf("key_names %d\n", key_count); const char *key_name; json_t *value; const char **key_names = (const char **)malloc(key_count * sizeof(char *)); int i = 0; json_object_foreach(root, key_name, value) { key_name = json_object_iter_key(value); key_names[i] = key_name; i++; } printf("key_names %s\n", key_names[2]); //int str_num = i; // 计算字符串数组中的字符串数量 /* char **sorted_names = sort_strings(key_names, key_count); char* stringA = (char*)malloc(1); // 初始化为一个空字符串 stringA[0] = '\0'; size_t len = 0; for (int i = 0; i < str_num; i++) { char* key = sorted_names[i]; json_t* value = json_object_get(root, key); char* str = json_dumps(value, JSON_ENCODE_ANY | JSON_COMPACT); len += strlen(key) + strlen(str) + 2; // 2 是键值对之间的字符 stringA = (char*)realloc(stringA, len); strcat(stringA, key); strcat(stringA, "="); strcat(stringA, str); strcat(stringA, "&"); free(str); } free(sorted_names); stringA[strlen(stringA) - 1] = '\0'; // 去掉最后一个"&" printf("stringA%s\n", stringA); unsigned char* sign = (unsigned char*)malloc(EVP_MAX_MD_SIZE); unsigned int sign_len = 0; HMAC(EVP_sha256(), key, strlen(key), (unsigned char*)stringA, strlen(stringA), sign, &sign_len); // 计算HMAC-SHA256签名 char* signature = (char*)malloc(sign_len * 2 + 1); // 签名的十六进制表示 signature[0] = '\0'; // 初始化为一个空字符串 for (int i = 0; i < sign_len; i++) { sprintf(signature + i * 2, "%02x", sign[i]); } json_object_set_new(root, "sign", json_string(signature)); // 在json中添加"sign"参数 json_dumpf(root, stdout, JSON_ENCODE_ANY | JSON_COMPACT); // 输出带有"sign"参数的json字符串 json_decref(root); free(key_names); free(stringA); free(sign); printf("signature%s\n", signature); */ return("A"); } int main() { char *key="39cabdfaab8c4da09bd6e9823c527836"; char *sss="{\"timestamp\":1685509898,\"sdkVersion\":\"1.0.30_1\",\"vin\":\"LJUBMSA24PKFFF198\"}"; calculate_signature(sss, key) ; }
代码中的问题在于 `json_load_file` 函数的第一个参数应该是一个文件名字符串,而不是 JSON 字符串。因此,需要将 `calculate_signature` 函数中的第一个参数 `json_str` 修改为文件名。
修改后的代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/hmac.h>
#include <jansson.h>
#include <time.h>
#include <errno.h>
#include <resolv.h>
#include <netdb.h>
char* calculate_signature(char* filename, char* key) {
json_t *root;
json_error_t error;
/* 从文件中读取 JSON 数据 */
root = json_load_file(filename, 0, &error);
if (!root) {
printf("Error in %s line %d: %s\n", error.source, error.line, error.text);
return NULL;
}
/* 遍历 JSON 对象中的所有键值对,并获取键的名称 */
int key_count = json_object_size(root);
printf("key_names %d\n", key_count);
const char *key_name;
json_t *value;
const char **key_names = (const char **)malloc(key_count * sizeof(char *));
int i = 0;
json_object_foreach(root, key_name, value) {
key_name = json_object_iter_key(value);
key_names[i] = key_name;
i++;
}
printf("key_names %s\n", key_names[2]);
//int str_num = i; // 计算字符串数组中的字符串数量
/*
char **sorted_names = sort_strings(key_names, key_count);
char* stringA = (char*)malloc(1); // 初始化为一个空字符串
stringA[0] = '\0';
size_t len = 0;
for (int i = 0; i < str_num; i++) {
char* key = sorted_names[i];
json_t* value = json_object_get(root, key);
char* str = json_dumps(value, JSON_ENCODE_ANY | JSON_COMPACT);
len += strlen(key) + strlen(str) + 2; // 2 是键值对之间的字符
stringA = (char*)realloc(stringA, len);
strcat(stringA, key);
strcat(stringA, "=");
strcat(stringA, str);
strcat(stringA, "&");
free(str);
}
free(sorted_names);
stringA[strlen(stringA) - 1] = '\0'; // 去掉最后一个"&"
printf("stringA%s\n", stringA);
unsigned char* sign = (unsigned char*)malloc(EVP_MAX_MD_SIZE);
unsigned int sign_len = 0;
HMAC(EVP_sha256(), key, strlen(key), (unsigned char*)stringA, strlen(stringA), sign, &sign_len); // 计算HMAC-SHA256签名
char* signature = (char*)malloc(sign_len * 2 + 1); // 签名的十六进制表示
signature[0] = '\0'; // 初始化为一个空字符串
for (int i = 0; i < sign_len; i++) {
sprintf(signature + i * 2, "%02x", sign[i]);
}
json_object_set_new(root, "sign", json_string(signature)); // 在json中添加"sign"参数
json_dumpf(root, stdout, JSON_ENCODE_ANY | JSON_COMPACT); // 输出带有"sign"参数的json字符串
json_decref(root);
free(key_names);
free(stringA);
free(sign);
printf("signature%s\n", signature);
*/
return("A");
}
int main() {
char *key="39cabdfaab8c4da09bd6e9823c527836";
char *filename="test.json";
calculate_signature(filename, key) ;
return 0;
}
```
另外,代码中还有一些注释掉的部分,需要先实现对应的函数才能使用。
char *result = NULL; char *slash = "/"; sprintf(result, "%s%s%d", ip, slash, prefixLen);修改如下代码
在修改前,需要注意 `result` 指针没有被分配内存,所以不能直接向其中写入数据。可以通过以下方式进行修改:
```c
char *result = NULL;
char slash[] = "/"; // 修改为字符数组,避免指针操作
int len = strlen(ip) + 1 + sizeof(slash) + 1; // 计算所需内存空间
result = malloc(len); // 分配内存
if (result == NULL) {
// 内存分配失败处理
return;
}
sprintf(result, "%s%s%d", ip, slash, prefixLen);
```
上述代码中,首先定义一个字符数组 `slash` 用于存储分隔符 `/`。然后通过计算所需内存空间,分配内存,并在分配失败时进行处理。最后使用 `sprintf` 函数将字符串拼接到 `result` 指针所指向的内存空间中。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)