英文:
I have created a function in C but it does not seem to make any changes to the variable I want to modify
问题
这是我在这个页面上的第一篇帖子,我几天前刚学会了C语言。
我在这段C代码中遇到了问题。思想是函数"create_test_buffer"修改变量"test_ptr",在每个位置上都添加一个值100,但在调用函数后再次打印"test_ptr"时,似乎什么也没有改变。
校验和似乎也没有被修改,理论上,如果在每个位置上添加一个值100,并且数组总共有512个位置,那么应该得到512*100=51200,但实际上显示的是6400。
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define TEST_BUFFER_LEN 512
#define NETWORK_SCAN_LIST_SIZE 8
typedef struct {
char ssid[33];
} wifi_ap_record_t;
wifi_ap_record_t ap_info_struct[NETWORK_SCAN_LIST_SIZE] =
{
{.ssid="SSID_3"},
{.ssid="SSID_1"},
{.ssid="SSID_1"},
{.ssid="SSID_1"},
{.ssid="SSID_1"},
{.ssid="SSID_2"},
{.ssid="SSID_1"},
{.ssid="SSID_1"},
};
uint16_t test_ptr[TEST_BUFFER_LEN];
uint16_t test_len = 0;
static void create_test_buffer(uint16_t *, uint16_t *);
static uint16_t calculate_checksum(uint16_t *, uint16_t);
int main()
{
printf("test_ptr before: %x\n", test_ptr);
printf("len before: %x\r\n", test_len);
create_test_buffer(test_ptr, &test_len);
printf("test_ptr after: %x\n", test_ptr);
printf("len after: %x\r\n", test_len);
uint16_t checksum = calculate_checksum(test_ptr, TEST_BUFFER_LEN);
printf("Checksum: %x\r\n", checksum);
return 0;
}
static void create_test_buffer(uint16_t *buff_ptr, uint16_t *len)
{
#define TEST_BUFFER_LEN 512
static uint16_t test_buffer[TEST_BUFFER_LEN];
for (uint16_t idx = 0; idx < TEST_BUFFER_LEN; ++idx) {
test_buffer[idx] = 0x64;
}
memcpy(buff_ptr, test_buffer, TEST_BUFFER_LEN);
// buff_ptr = test_buffer;
len = TEST_BUFFER_LEN;
}
static uint16_t calculate_checksum(uint16_t *data, uint16_t len)
{
uint16_t idx = 0;
uint16_t checksum = 0;
while (idx < len) {
checksum += data[idx];
idx++;
}
return checksum;
}
至于函数部分,我已经尝试更改参数修改的方式,首先尝试了"len = TEST_BUFFER_LEN;",然后尝试了"memcpy(buff_ptr, test_buffer, TEST_BUFFER_LEN);",后者至少允许我看到校验和的变化。
英文:
this is my first post on this page, I just learned the C language a few days ago.
I have a problem with this C code, the idea is that the function "create_test_buffer" modifies the variable "test_ptr" adding a value of 100 in each position, but when I print "test_ptr" again after calling the function, nothing seems to have changed.
The checksum doesn't seem to be modified either, it is supposed that if you add a value of 100 in each position and in total the array has 512 positions you should get 512*100=51200, but instead it is showing 6400.
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define TEST_BUFFER_LEN 512
#define NETWORK_SCAN_LIST_SIZE 8
typedef struct {
char ssid[33];
} wifi_ap_record_t;
wifi_ap_record_t ap_info_struct[NETWORK_SCAN_LIST_SIZE] =
{
{.ssid="SSID_3"},
{.ssid="SSID_1"},
{.ssid="SSID_1"},
{.ssid="SSID_1"},
{.ssid="SSID_1"},
{.ssid="SSID_2"},
{.ssid="SSID_1"},
{.ssid="SSID_1"},
};
uint16_t test_ptr[TEST_BUFFER_LEN];
uint16_t test_len = 0;
static void create_test_buffer(uint16_t *, uint16_t *);
static uint16_t calculate_checksum(uint16_t *, uint16_t);
int main()
{
printf("test_ptr before: %x\n", test_ptr);
printf("len before: %x\r\n", test_len);
create_test_buffer(test_ptr, &test_len);
printf("test_ptr after: %x\n", test_ptr);
printf("len after: %x\r\n", test_len);
uint16_t checksum = calculate_checksum(test_ptr, TEST_BUFFER_LEN);
printf("Checksum: %x\r\n", checksum);
return 0;
}
static void create_test_buffer(uint16_t *buff_ptr, uint16_t *len)
{
#define TEST_BUFFER_LEN 512
static uint16_t test_buffer[TEST_BUFFER_LEN];
for (uint16_t idx = 0 ; idx < TEST_BUFFER_LEN; ++idx) {
test_buffer[idx] = 0x64;
}
memcpy(buff_ptr, test_buffer, TEST_BUFFER_LEN);
// buff_ptr = test_buffer;
len = TEST_BUFFER_LEN;
}
static uint16_t calculate_checksum(uint16_t *data, uint16_t len)
{
uint16_t idx = 0;
uint16_t checksum = 0;
while (idx < len) {
checksum += data[idx];
idx++;
}
return checksum;
}
as for the functions I have tried to change the way the modification of the parameters is done, first I tried with "len = TEST_BUFFER_LEN;" and then with "memcpy(buff_ptr, test_buffer, TEST_BUFFER_LEN);" being this last one the one that at least allows me to see a change in the checksum.
答案1
得分: 0
你忘记解引用指向 "len" 的指针。
*len = TEST_BUFFER_LEN;
这意味着"写入到 len 指向的变量"。如果忘记了 `*`,你会覆盖指针本身,而不是指针指向的内容。
你的代码中还有至少一个错误 - `memcpy` 接受以字节为单位的长度,而不是16位字。所以在将其传递给 memcpy 之前,你必须将长度乘以每个元素的大小。
memcpy(buff_ptr, test_buffer, TEST_BUFFER_LEN * sizeof(uint16_t));
你的编译器很可能警告了你这两点。你应该认真对待编译器的警告,最好启用所有的警告(例如,在GCC/Clang上使用 `-Wall -Wextra`)。
英文:
You forgot to dereference the pointer to "len".
*len = TEST_BUFFER_LEN;
This means "Write to the variable that len points to". If you forget the *
, you overwrite the pointer, and not the thing the pointer points to.
You've also got at least one more mistake in the code - memcpy
takes the length in bytes, not 16-bit words, so you have to multiply the length with the size of each element before passing that to memcpy.
memcpy(buff_ptr, test_buffer, TEST_BUFFER_LEN * sizeof(uint16_t));
Your compiler likely warned you about both of these. You should take compiler warnings seriously, and ideally, enable all of them (i.e. -Wall -Wextra
on GCC/Clang)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论