gpt4 book ai didi

C - 将数组局部复制到全局 - 段错误

转载 作者:太空宇宙 更新时间:2023-11-04 08:16:48 25 4
gpt4 key购买 nike

<分区>

我想要一个全局数组,可以为每个“菜单选择”复制到本地数组。您可以在选择 1 中指定数组的大小,我觉得当您将本地数组复制到全局数组时会弄乱全局数组的大小。当尝试在“菜单 1”中完成此“复制”时,如果您的“金额”过高 (10,000+),您将收到“段错误(核心已转储)”错误。

此外,当退出程序时,您会收到“段错误(核心已转储)”代码 139 错误。

所以我的问题是如何复制一个数组,我仍然可以拥有一个“全局数组”并且不会遇到这些错误。我包含了一些 sizeof array printf 来检查大小,因为就像我上面说的,我认为在复制发生之前需要做一些事情来改变数组的大小。

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <inttypes.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>

int main()
{

int choice = 0; // menu variable
int global_amount = 0; //random number generator var's
int globalArray[0];

do
{
system("clear");
printf("Menu\n");
printf("1: Random Number Generator\n");
printf("2: Bubble Sort\n");
printf("3: Exit\n");
printf("\nNote: Only Random Number Generator Writes To Global Array!\n");
printf("\nMenu Choice: ");
scanf ("%d", &choice);

if (choice == 1)
{
system("clear");
printf("Random Number Generator\n");
printf("---------\n");

printf("Enter the amount of numbers you want (range will be from 0-99): ");
scanf ("%d", &global_amount);

int highest = 99;

// initilize array with size "amount" specified above
int randArray[global_amount];

int i, randomNumber;

for(i=0;i<global_amount;i++)
{
randomNumber = rand() % highest;
randArray[i] = randomNumber;
}

printf("\nArray Filled With Random Numbers 0 through %d\n\n", highest);

printf("\nSize of arrays before copy\n");
printf("randArray - %zu\n", sizeof(randArray));
printf("globalArray - %zu\n", sizeof(globalArray));

// copy local array to global array
for(i=0;i<global_amount;i++)
{
globalArray[i] = randArray[i];
}

printf("\nSize of arrays after copy\n");
printf("randArray - %zu\n", sizeof(randArray));
printf("globalArray - %zu\n", sizeof(globalArray));

// prints out your array
printf("\nYour array has these values: \n");
for(i=0;i<global_amount;i++)
{
printf("%d ", globalArray[i]);
}
printf("\n");

printf("\nPress enter to continue...\n");
char var;scanf ("%c", &var);scanf ("%c", &var);
}

else if (choice == 2)
{
system("clear");
printf("Bubble Sort\n");
printf("---------\n");

int i;
int bubbleArray[global_amount]; // local array

// copy global array to local array
for(i=0;i<global_amount;i++)
{
bubbleArray[i] = globalArray[i];
}

// display array to be sorted
printf("Your array had these values: \n");
for(i=0;i<global_amount;i++)
{
printf("%d ", bubbleArray[i]);
}

// bubble sort code
int c, d, swap;

for (c = 0 ; c < ( global_amount - 1 ); c++)
{
for (d = 0 ; d < global_amount - c - 1; d++)
{
if (bubbleArray[d] > bubbleArray[d+1]) /* For decreasing order use < */
{
swap = bubbleArray[d];
bubbleArray[d] = bubbleArray[d+1];
bubbleArray[d+1] = swap;
}
}
}

printf("\n\nBubble Sort Completed\n");

// display sorted array
printf("\n");
printf("Sorted array list in ascending order:\n");
for ( c = 0 ; c < global_amount ; c++ )
{
printf("%d ", bubbleArray[c]);
}
printf("\n");

printf("\nPress enter to continue...\n");
char var;scanf ("%c", &var);scanf ("%c", &var);
}

else if (choice == 3)
{
system("clear");
return 0;
}

else
{
system("clear");
printf("That is not a valid entry.\n");
printf("Press enter to continue...\n");
char var;scanf ("%c", &var);scanf ("%c", &var);
}
}
while (choice !=3);

return 0;
}

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