我试图编写一个泛型函数来获取整数或字符串数组中的最小元素。 我正在使用记忆功能。 以下是我写的代码:
编辑:修改过的代码 - 我从int size_t更改了int size
/* Write a function that returns minimum values of an array of integers or strings */ #include <stdio.h> #include <stdlib.h> #include <string.h> void returnMinAddress(void *a, void *b, int arr_size, int size) { b = a; for (int i = 0; i < arr_size; i++) { if (memcmp(b, a+((i)*size), size) < 0) { memmove(b, a+((i)*size), size); } } } int main() { void *b = malloc(sizeof(int)); /* For an array of type integer */ int a[8] = {3, 2, 1, -4, 6, 9, 8, -1}; returnMinAddress(a, b, 8, sizeof(int)); printf("The result is : %d\n", *(int *)b); free(b); return 0; }花了很多时间后,我无法理解为什么我一直把答案保持为0 ..以下是输出的截图。 我在这里想念的是什么?
原始代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> void returnMinAddress(void *a, void *b, int arr_size, int size_t) { b = a; for (int i = 0; i < arr_size; i++) { if (memcmp(b, a+((i)*size_t), size_t) < 0) { memmove(b, a+((i)*size_t), size_t); } } } int main() { void *b = malloc(sizeof(int)); /* For an array of type integer */ int a[8] = {3, 2, 1, -4, 6, 9, 8, -1}; returnMinAddress(a, b, 8, sizeof(int)); printf("The result is : %d\n", *(int *)b); free(b); return 0; }
I am trying to write a generic function to obtain the minimum element in an array of integers or strings. I am using memory functions to do so. The following is the code which I wrote:
Edit: Modified code - I changed int size from int size_t
/* Write a function that returns minimum values of an array of integers or strings */ #include <stdio.h> #include <stdlib.h> #include <string.h> void returnMinAddress(void *a, void *b, int arr_size, int size) { b = a; for (int i = 0; i < arr_size; i++) { if (memcmp(b, a+((i)*size), size) < 0) { memmove(b, a+((i)*size), size); } } } int main() { void *b = malloc(sizeof(int)); /* For an array of type integer */ int a[8] = {3, 2, 1, -4, 6, 9, 8, -1}; returnMinAddress(a, b, 8, sizeof(int)); printf("The result is : %d\n", *(int *)b); free(b); return 0; }After spending good amount of time, I fail to understand why do I keep getting my answer as 0.. The following is the screenshot of the output. What am I missing here?
Original Code:
#include <stdio.h> #include <stdlib.h> #include <string.h> void returnMinAddress(void *a, void *b, int arr_size, int size_t) { b = a; for (int i = 0; i < arr_size; i++) { if (memcmp(b, a+((i)*size_t), size_t) < 0) { memmove(b, a+((i)*size_t), size_t); } } } int main() { void *b = malloc(sizeof(int)); /* For an array of type integer */ int a[8] = {3, 2, 1, -4, 6, 9, 8, -1}; returnMinAddress(a, b, 8, sizeof(int)); printf("The result is : %d\n", *(int *)b); free(b); return 0; }
最满意答案
您的通用功能无效。
首先,尽管size_t不是关键字,但它是一个类型说明符。 因此,将此单词用作标识符是一个坏主意。
这个说法
b = a;应该换成
memmove( b, a, size_t );其次根据C标准,您可能不会将指针算法应用于void *类型的指针,因为void类型是不完整的类型。 所以你应该写一些例子
( char * )a + i * size_t代替
a+((i)*size_t)由于函数搜索最小值,然后搜索此语句中的条件
if (memcmp(b, a+((i)*size_t), size_t) < 0) {应该写得像
if ( memcmp( ( char * )a + i * size_t, b, size_t ) < 0)考虑到所有这些功能可能看起来像
void returnMinAddress( void *a, void *b, size_t n, size_t m) { memmove(b, a, m); for (size_t i = 1; i < n; i++) { if ( memcmp( ( char * )a + i * m, b, m ) < 0 ) { memmove(b, ( char * )a + i * m, m ); } } }但是,还有一个问题是该功能无法解决。 被认为是原始字节序列的负整数可能大于正数。 因此,如果您将尝试上面显示的数组函数
int a[8] = { 3, 2, 1, -4, 6, 9, 8, -1 };你会得到最小值等于1而实际上它等于-4 。
编写这种通用函数的方法之一如下
#include <stdio.h> void * returnMinAddress( const void *a, size_t n, size_t m, int cmp( const void *, const void *) ) { const void *min = a; for (size_t i = 1; i < n; i++) { if ( cmp( ( const char * )a + i * m, min ) < 0 ) { min = (const char *)a + i * m; } } return (void *)min; } int cmp_int(const void *p1, const void *p2) { int a = *(const int *)p1; int b = *(const int *)p2; return (b < a) - (a < b); } int main( void ) { int a[8] = { 3, 2, 1, -4, 6, 9, 8, -1 }; int *b = returnMinAddress(a, 8, sizeof(int), cmp_int); printf("The result is : %d\n", *b); }程序输出是
The result is : -4Your generic function is invalid.
First of all though size_t is not a keyword nevertheless it is a type specifier. So it is a bad idea to use this word as an identifier.
This statement
b = a;should be replaced with
memmove( b, a, size_t );Secondly according to the C Stndard you may not apply the pointer arithmetic to pointers of type void * because the type void is an incomplete type. So you should write for example
( char * )a + i * size_tinstead of
a+((i)*size_t)As the function searches the minimum then the condition in this statement
if (memcmp(b, a+((i)*size_t), size_t) < 0) {should be written like
if ( memcmp( ( char * )a + i * size_t, b, size_t ) < 0)Taking all this into account the function could look like
void returnMinAddress( void *a, void *b, size_t n, size_t m) { memmove(b, a, m); for (size_t i = 1; i < n; i++) { if ( memcmp( ( char * )a + i * m, b, m ) < 0 ) { memmove(b, ( char * )a + i * m, m ); } } }However there is one more problem that the function can not resolve. Negative integers considered like a sequence of raw bytes can be greater than positive numbers. So if you will try the function shown above for your array
int a[8] = { 3, 2, 1, -4, 6, 9, 8, -1 };you will get that the minimum is equal to 1 while in realty it is equal to -4.
One of approaches to write such a generic function is the following
#include <stdio.h> void * returnMinAddress( const void *a, size_t n, size_t m, int cmp( const void *, const void *) ) { const void *min = a; for (size_t i = 1; i < n; i++) { if ( cmp( ( const char * )a + i * m, min ) < 0 ) { min = (const char *)a + i * m; } } return (void *)min; } int cmp_int(const void *p1, const void *p2) { int a = *(const int *)p1; int b = *(const int *)p2; return (b < a) - (a < b); } int main( void ) { int a[8] = { 3, 2, 1, -4, 6, 9, 8, -1 }; int *b = returnMinAddress(a, 8, sizeof(int), cmp_int); printf("The result is : %d\n", *b); }The program output is
The result is : -4更多推荐
发布评论