传递数组指针给函数后如何取值?梳排序函数读取数组异常排查
Hey there! Let's break down what's going wrong with your comb sort function and clear up how to properly access array values when passing pointers around.
First, Let's Diagnose the Pointer Reading Problem
From what you described—your gdb shows the pointer only points to 1 even though the original array has all expected values—this is almost always a type mismatch between how you're passing the array and how your function is receiving it, or a mistake in how you're accessing the pointer.
Here are the most likely culprits:
- You're passing the array's address (e.g.,
&numbers) instead of the array name itself, but your function expects anint*. The array namenumbersalready decays to a pointer to its first element (int*), so passing&numbersgives you anint (*)[N](a pointer to an array of size N), which is incompatible. If you try to dereference this directly, you might end up with unexpected values. - Your function parameter is declared as
int**(a pointer to a pointer) instead ofint*, but you're passing the array name. Dereferencing anint**that points to anint*would give you the first element's value—but if you're not handling this correctly, you might be reading memory incorrectly. - You accidentally modified the pointer inside the function (e.g., doing
arr++without resetting it), so it's no longer pointing to the start of the array.
How to Properly Access Array Values After Passing a Pointer
In C, arrays and pointers are closely linked, but there are two common ways to pass array data to a function—let's cover both:
1. Passing the array as an int* (most common)
When you pass the array name (e.g., comb_sort(numbers, length)), it automatically decays to a pointer to the first element of the array. Your function should declare the parameter as int* arr, and you can access elements in two equivalent ways:
- Using array notation:
arr[index](this is syntactic sugar, and the compiler converts it to pointer arithmetic) - Using pointer arithmetic:
*(arr + index)(directly dereferences the pointer offset byindexelements)
Example of a correctly structured comb sort function:
#include <stdbool.h> void comb_sort(int* arr, int length) { int gap = length; bool swapped = true; // Comb sort logic while (gap != 1 || swapped) { // Shrink gap by 1.3 (standard comb sort factor) gap = (gap * 10) / 13; if (gap < 1) gap = 1; swapped = false; for (int i = 0; i < length - gap; i++) { // Access array elements normally with arr[i] if (arr[i] > arr[i + gap]) { // Swap elements int temp = arr[i]; arr[i] = arr[i + gap]; arr[i + gap] = temp; swapped = true; } } } } // Main function call example int main() { int numbers[] = {3, 1, 4, 1, 5, 9, 2, 6}; int length = sizeof(numbers) / sizeof(numbers[0]); comb_sort(numbers, length); // Pass array name (decays to int*) // Print sorted array... return 0; }
2. Passing a pointer to the entire array (less common)
If you need to preserve the array's size information (e.g., for compile-time checks), you can pass a pointer to the array. The function parameter would look like int (*arr)[N] where N is the array's fixed size. To access elements, you first dereference the pointer to get the array, then use subscript notation:
#define ARRAY_SIZE 8 void comb_sort_fixed(int (*arr)[ARRAY_SIZE], int length) { // Access elements with (*arr)[index] if ((*arr)[0] > (*arr)[1]) { // Swap logic... } } // Call like this: comb_sort_fixed(&numbers, ARRAY_SIZE);
Quick Fixes for Your Debugging Issue
- Check parameter types: Make sure your function expects
int*and you're passing the array name (not&numbers). - Verify pointer position: In gdb, check if the pointer
arris pointing to the start of the array (compare it tonumbersin main). If it's not, you might have modified the pointer inside the function. - Always pass the array length: Arrays lose their size information when passed as pointers, so you must explicitly pass the length to avoid out-of-bounds access (which can cause garbage values like the
1you're seeing).
内容的提问来源于stack exchange,提问作者Harrison

