gpt4 book ai didi

c - 变量值如何改变

转载 作者:太空宇宙 更新时间:2023-11-04 06:46:43 32 4
gpt4 key购买 nike

据我所知,当我们想要在函数外部更改变量的值时,我们必须使用指针,如下所示: https://www.tutorialspoint.com/cprogramming/c_function_call_by_value.htm .但我不明白为什么我能够在函数内更改变量 name 的值。

#include<stdio.h>
#include<string.h>
#pragma warning(disable:4996)
#define MaxNameSize 14
#define MaxNamebuffer MaxNameSize+1
#define MaxNames 8

int main()
{

void StringInsertionSort(int, int, int max, char [][MaxNamebuffer]);
char name[MaxNames][MaxNamebuffer] = { "Taylor, Victor","Duncan, Denise","Ramdhan, Kamal","Singh, Krishna","Ali, Mechael","Sawh, Anisa","Khan, Carol","Owen, David" };
StringInsertionSort(0, MaxNames - 1, MaxNamebuffer, name);
printf("\nThe sorted names are:\n\n");
for (int h = 0; h < MaxNames - 1; h++)
{
printf("%s \n", name[h]);
}
return(0);
}


void StringInsertionSort(int lo,int hi,int max, char list[][MaxNamebuffer])
{
char key[MaxNamebuffer];
for (int h = lo + 1; h <= hi; h++)
{
strcpy(key, list[h]);
int k = h - 1;
while (k >= lo && strcmp(key, list[k]) < 0)
{
strcpy(list[k + 1], list[k]);
--k;
}
strcpy(list[k + 1], key);
}

}

最佳答案

在代码的参数上下文中,char list[][MaxNamebuffer]char (*list)[MaxNamebuffer] 100% 相同,并且都表示 list 是指向 MaxNamebuffer 字节数组的指针

关于c - 变量值如何改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57047171/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com