C memcpy 交换二维数组的行
C memcpy to swap rows of 2D array
我正在尝试使用 memcpy
C 库函数来交换二维数组(字符串数组)的行。此任务的源文件如下:
main.c
#include <stdlib.h>
#include "main.h"
char *table[NBLOCK] = {
"abcdefghi",
"defghiabc",
"ghiabcdef",
"bcaefdhig",
"efdhigbca",
"higbcaefd",
"cabfdeigh",
"fdeighcab",
"ighcabfde",
};
int main() {
swap_rows(table, 0, 2);
return 0;
}
main.h
#define NBLOCK 9
#define BLOCK_CELLS 9
void swap_rows(char**, int, int);
shuffle.c
#include <string.h>
#include "main.h"
void swap_rows(char **table, int r1, int r2) {
char tmp[BLOCK_CELLS];
size_t size = sizeof(char) * BLOCK_CELLS;
memcpy(tmp, table[r1], size);
memcpy(table[r1], table[r2], size); /* SIGSEGV here */
memcpy(table[r2], tmp, size);
}
swap_rows
函数内部发生段错误。在上面显示的三个 memcpy
调用中,第一个按预期工作。我注释掉了最后两个 memcpy
调用并添加了以下行:
table[0][0] = 'z';
但是,段错误又出现了。为什么不允许我在 swap_rows
函数中覆盖 table
的值?
您不能修改字符串文字。
有关详细信息,请参阅 c - Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?。
您可以修改指针的值以交换行。
void swap_rows(char **table, int r1, int r2) {
char* tmp;
tmp = table[r1];
table[r1] = table[r2];
table[r2] = tmp;
}
如果您更喜欢使用memcpy()
:
void swap_rows(char **table, int r1, int r2) {
char* tmp;
size_t size = sizeof(tmp);
memcpy(&tmp, &table[r1], size);
memcpy(&table[r1], &table[r2], size);
memcpy(&table[r2], &tmp, size);
}
在你的代码中 table
不是 定义为 char
的二维数组,它是指向 char
初始化的指针数组带有指向字符串文字的指针,不得修改。
您遇到分段错误,因为字符串文字存储在受操作系统保护的 read-only 内存中。
您应该交换 swap_rows
中的指针,或者将 table
定义为真正的二维数组并用适当的原型交换行:
#include <stdlib.h>
//#include "main.h"
#define NBLOCK 9
#define BLOCK_CELLS 9
void swap_rows(char table[][BLOCK_CELLS], int, int);
char table[NBLOCK][BLOCK_CELLS] = {
"abcdefghi",
"defghiabc",
"ghiabcdef",
"bcaefdhig",
"efdhigbca",
"higbcaefd",
"cabfdeigh",
"fdeighcab",
"ighcabfde",
};
int main() {
swap_rows(table, 0, 2);
return 0;
}
void swap_rows(char table[][BLOCK_CELLS], int r1, int r2) {
char tmp[BLOCK_CELLS];
size_t size = sizeof(tmp);
memcpy(tmp, table[r1], size);
memcpy(table[r1], table[r2], size);
memcpy(table[r2], tmp, size);
}
我正在尝试使用 memcpy
C 库函数来交换二维数组(字符串数组)的行。此任务的源文件如下:
main.c
#include <stdlib.h>
#include "main.h"
char *table[NBLOCK] = {
"abcdefghi",
"defghiabc",
"ghiabcdef",
"bcaefdhig",
"efdhigbca",
"higbcaefd",
"cabfdeigh",
"fdeighcab",
"ighcabfde",
};
int main() {
swap_rows(table, 0, 2);
return 0;
}
main.h
#define NBLOCK 9
#define BLOCK_CELLS 9
void swap_rows(char**, int, int);
shuffle.c
#include <string.h>
#include "main.h"
void swap_rows(char **table, int r1, int r2) {
char tmp[BLOCK_CELLS];
size_t size = sizeof(char) * BLOCK_CELLS;
memcpy(tmp, table[r1], size);
memcpy(table[r1], table[r2], size); /* SIGSEGV here */
memcpy(table[r2], tmp, size);
}
swap_rows
函数内部发生段错误。在上面显示的三个 memcpy
调用中,第一个按预期工作。我注释掉了最后两个 memcpy
调用并添加了以下行:
table[0][0] = 'z';
但是,段错误又出现了。为什么不允许我在 swap_rows
函数中覆盖 table
的值?
您不能修改字符串文字。 有关详细信息,请参阅 c - Why do I get a segmentation fault when writing to a "char *s" initialized with a string literal, but not "char s[]"?。
您可以修改指针的值以交换行。
void swap_rows(char **table, int r1, int r2) {
char* tmp;
tmp = table[r1];
table[r1] = table[r2];
table[r2] = tmp;
}
如果您更喜欢使用memcpy()
:
void swap_rows(char **table, int r1, int r2) {
char* tmp;
size_t size = sizeof(tmp);
memcpy(&tmp, &table[r1], size);
memcpy(&table[r1], &table[r2], size);
memcpy(&table[r2], &tmp, size);
}
在你的代码中 table
不是 定义为 char
的二维数组,它是指向 char
初始化的指针数组带有指向字符串文字的指针,不得修改。
您遇到分段错误,因为字符串文字存储在受操作系统保护的 read-only 内存中。
您应该交换 swap_rows
中的指针,或者将 table
定义为真正的二维数组并用适当的原型交换行:
#include <stdlib.h>
//#include "main.h"
#define NBLOCK 9
#define BLOCK_CELLS 9
void swap_rows(char table[][BLOCK_CELLS], int, int);
char table[NBLOCK][BLOCK_CELLS] = {
"abcdefghi",
"defghiabc",
"ghiabcdef",
"bcaefdhig",
"efdhigbca",
"higbcaefd",
"cabfdeigh",
"fdeighcab",
"ighcabfde",
};
int main() {
swap_rows(table, 0, 2);
return 0;
}
void swap_rows(char table[][BLOCK_CELLS], int r1, int r2) {
char tmp[BLOCK_CELLS];
size_t size = sizeof(tmp);
memcpy(tmp, table[r1], size);
memcpy(table[r1], table[r2], size);
memcpy(table[r2], tmp, size);
}